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

前端 未结 2 1519
梦毁少年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:45

    Some extra details if you want to tweak the default message formatting at the same time:

    use Monolog\Logger;
    use Monolog\Formatter\LineFormatter;
    use Monolog\Handler\StreamHandler;
    
    $output = "[%datetime%] %channel%.%level_name%: %message%\n";
    $formatter = new LineFormatter($output);
    
    $streamHandler = new StreamHandler('php://stdout', Logger::DEBUG);
    $streamHandler->setFormatter($formatter);
    
    $logger = new Logger('LoggerName');
    $logger->pushHandler($streamHandler);
    

提交回复
热议问题