在执行Symfony命令期间,我想将消息记录到另一个文件中。我已经阅读了Symfony和Monolog文档,它应该像我在这里描述的那样工作。(注意,我知道来自"条令"、"事件"…通道的消息仍将由主处理程序记录,但这对我来说无关紧要)
在我的config.yml
中,我有这个:
monolog:
channels: [commandline]
handlers:
main:
type: stream
path: "%kernel.logs_dir%/%kernel.environment%.main.log"
level: debug
channels: [!commandline]
commandline:
type: stream
path: "%kernel.logs_dir%/%kernel.environment%.commandline.log"
level: debug
channels: commandline
stdout:
type: stream
path: "php://stdout"
level: debug
channels: commandline
mail:
type: stream
action_level: alert
handler: buffered_mail
buffered_mail:
type: buffer
handler: swift
swift:
type: swift_mailer
from_email: some@email.com
to_email: some@email.com
subject: "Something went wrong"
level: alert
我希望有两个日志文件:dev.main.log
和dev.commandline.log
。但我仍然有第三个日志文件:dev.log
,它记录所有消息。我似乎不知道日志处理程序是在哪里定义的,也不知道如何防止它记录事情。。。
如果有人能给我指明正确的方向,那就太好了!
顺便说一句,我使用的是:
- symfony 2.3
- 独白束2.4
编辑
config_dev.yml
中没有monolog
部分
从config_dev.yml
中删除monolog.handlers.main
。
它通常包含path: "%kernel.logs_dir%/%kernel.environment%.log"
config_dev.yml(默认)
monolog:
handlers:
main: # <- remove this handler
type: stream
path: "%kernel.logs_dir%/%kernel.environment%.log" #<- logs/dev.log
level: debug
从该配置文件中删除main
处理程序。
如果有人遇到这种情况,并且仍然对发生这种情况的原因感兴趣,则会在''Symfony''Bundle''MonologBundle''DependencyInjection''Compiler''DebugHandlerPass::process()方法中注入调试处理程序。。。
class DebugHandlerPass implements CompilerPassInterface
{
// ...
public function process(ContainerBuilder $container)
{
if (!$container->hasDefinition('profiler')) {
return;
}
if (!$container->getParameter('kernel.debug')) {
return;
}
$debugHandler = new Definition('%monolog.handler.debug.class%', array(Logger::DEBUG, true));
$container->setDefinition('monolog.handler.debug', $debugHandler);
foreach ($this->channelPass->getChannels() as $channel) {
$container
->getDefinition($channel === 'app' ? 'monolog.logger' : 'monolog.logger.'.$channel)
->addMethodCall('pushHandler', array(new Reference('monolog.handler.debug')));
}
}
}
正如您所看到的,这将向每个注册的通道推送一个新的处理程序,从而覆盖可能已经添加的任何其他处理程序。