phploggingzend-framework3zend-log

zend-log: Send errors to stderr, anything else to stdout


Currently my logger is set up like this:

$logger = new Zend\Log\Logger();
$logger->addWriter(new Zend\Log\Writer\Stream('php://stdout'));

The downside: Both $logger->err() and $logger->info() now print to stdout. However, I’d like the former to print to stderr? Can I assign certain writers to priorities? So anything <= Zend\Log\Logger::ERR ends up in stderr while anything else in stdout?


Solution

  • You can add as many writers as you want, and filter them by their priority.

    // Add ERROR writer
    $logger = new Logger();
    $errorWriter = new Stream('php://stdout');
    // Filter logs only for ERROR priority
    $errorWriter->addFilter(new Priority(Logger::ERR, '='));
    $logger->addWriter($errorWriter);
    
    // Add INFO writer
    $infoWriter = new Stream('php://stderr');
    // Filter logs only for INFO priority
    $infoWriter->addFilter(new Priority(Logger::INFO, '='));
    $logger->addWriter($infoWriter);
    

    You can find more information in the docs: