0

While using monolog in Symfony's console commands all messages are being outputed to stderr. How to configure monolog make it send everything below WARNING level to stdout and everything else to stderr?

Xander
  • 1,114
  • 2
  • 9
  • 18
  • 2
    Have you read this page : http://symfony.com/doc/current/logging/monolog_console.html ? – Ugo T. Sep 22 '16 at 15:02
  • Of course I did. It says: "Additionally, error logs are written to the error output (php://stderr). ". The problem is - this is not true. I'm saving stdout to one file and stderr to another. Whatever I'm outputing using Symfony's OutputInterface::writeln ends up in the first one. Everything I'm logging using monolog (including INFO) ends up in the second one. – Xander Sep 23 '16 at 07:23

1 Answers1

1

The easies way is to look how Monolog's (Symfony's actually, from bridge) ConsoleHandler works:

https://github.com/symfony/monolog-bridge/blob/master/Handler/ConsoleHandler.php

   /**
     * Before a command is executed, the handler gets activated and the console output
     * is set in order to know where to write the logs.
     *
     * @param ConsoleCommandEvent $event
     */
    public function onCommand(ConsoleCommandEvent $event)
    {
        $output = $event->getOutput();
        if ($output instanceof ConsoleOutputInterface) {
            $output = $output->getErrorOutput();
        }
        $this->setOutput($output);
    }

So you can extend and override this behavior to store both outputs and decide which use here

https://github.com/symfony/monolog-bridge/blob/master/Handler/ConsoleHandler.php#L153-L160

     /**
     * {@inheritdoc}
     */
    protected function write(array $record)
    {
        // at this point we've determined for sure that we want to output the record, so use the output's own verbosity
        $this->output->write((string) $record['formatted'], false, $this->output->getVerbosity());
    }

For example you can check $record['level'] and switch the output manualy. After you implement your own handler you can configure it with monolog brige configuration:

http://symfony.com/doc/current/logging/monolog_console.html

# app/config/config.yml
monolog:
    handlers:
        console:
            type: service
            id: my_app.monolog.console_handler
ScayTrase
  • 1,810
  • 23
  • 36