Question

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?

Was it helpful?

Solution 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

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top