在Symfony的控制台命令中使用monolog时,所有消息都被输出到stderr。如何配置monolog使它将警告级别以下的所有内容发送到stdout,其他所有内容都发送到stderr?
答案 0 :(得分:1)
简单的方法是看看Monolog的(Symfony实际上来自桥梁)ConsoleHandler是如何工作的:
https://github.com/symfony/monolog-bridge/blob/master/Handler/ConsoleHandler.php
/**
* Before a command is executed, the handler gets activated and the console output
* is set in order to know where to write the logs.
*
* @param ConsoleCommandEvent $event
*/
public function onCommand(ConsoleCommandEvent $event)
{
$output = $event->getOutput();
if ($output instanceof ConsoleOutputInterface) {
$output = $output->getErrorOutput();
}
$this->setOutput($output);
}
因此,您可以扩展和覆盖此行为以存储两个输出并决定在何处使用
https://github.com/symfony/monolog-bridge/blob/master/Handler/ConsoleHandler.php#L153-L160
/**
* {@inheritdoc}
*/
protected function write(array $record)
{
// at this point we've determined for sure that we want to output the record, so use the output's own verbosity
$this->output->write((string) $record['formatted'], false, $this->output->getVerbosity());
}
例如,您可以检查$record['level']
并手动切换输出。实现自己的处理程序后,可以使用monolog brige配置对其进行配置:
http://symfony.com/doc/current/logging/monolog_console.html
# app/config/config.yml
monolog:
handlers:
console:
type: service
id: my_app.monolog.console_handler