I'm creating a custom command, and I want to use a different log file.

In the docs it says that I have to tag my service. But my custom command isn't a service, is it? Should I make it a service?

有帮助吗?

解决方案 2

First I pushed NullHandler to invalidate the other handlers. Then I added one handler for info messages, and another one for all messages, that only triggers if there is a warning, error, etc.

$logger = $this->getContainer()->get('logger');
$logger->pushHandler(new NullHandler);
$logger->pushHandler(new StreamHandler('app/logs/mycommand.log', Logger::DEBUG));
//$logger->pushHandler(new FingersCrossedHandler(new StreamHandler('app/logs/mycommand.log', Logger::DEBUG), Logger::WARNING, 0, false)); // does not work. It prevents INFO messages from being logged

其他提示

The second document is for the case when you want to create a custom logger. But if you just want to change the file where the log is saved, you don´t need to do that, you just need to set the monolog 'path' configuration. For more info, see this

Also, The object you retrieve when you get('logger') is derived from the class Monolog\Logger. That class has two methods: pushHandler() to add a handler to the handler stack and popHandler() to remove a handler to the handler stack. Maybe in your command you could pop all the standard handlers and then push an instance of Monolog\Handler\StreamHandler with your custom path.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top