PHP: How to use monolog to log to console (php://out)?

前端 未结 2 1560
梦毁少年i
梦毁少年i 2021-02-03 20:11

I just switched to monolog and wanted to log my message to the PHP console instead of a file. This might seem obvious for some people, but it took me a little while to figure ou

2条回答
  •  眼角桃花
    2021-02-03 20:47

    The solution is rather simple. Since the example shows a StreamHandler it's possible to pass in a stream (instead of the path to a file). By default, everything that is echo'ed in PHP is written to php://stdout / php://output so we can simple use one of those as stream for the StreamHandler:

    pushHandler(new StreamHandler('php://stdout', Logger::WARNING)); // <<< uses a stream
    
    // add records to the log
    $log->warning('Foo');
    $log->error('Bar');
    

    Hope this saves somebody some time :)

提交回复
热议问题