0

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?

Elnur Abdurrakhimov
  • 44,533
  • 10
  • 148
  • 133
ChocoDeveloper
  • 14,160
  • 26
  • 79
  • 117

2 Answers2

1

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.

Carlos Granados
  • 11,273
  • 1
  • 38
  • 44
  • But I want to use dev.log/app.log for http requests, and command.log for my custom command. Inside my command I do `$this->getContainer()->get('logger')`. So how do I tell it to use a different handler there? Even if I configure it like in your link I will have to do something else. – ChocoDeveloper Sep 16 '12 at 11:08
0

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
ChocoDeveloper
  • 14,160
  • 26
  • 79
  • 117