문제

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