Search code examples
phploggingsymfonysymfony-2.1monolog

How to log to a different file inside a Command?


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?


Solution

  • 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