1 安装 composer require monolog/monolog 2 使用 // 创建日志服务 $logger = new Logger('my_logger’); // 定义一个handle $stream = new StreamHandler(__DIR__.'/log/hello.log', Logger::DEBUG); // 设置输出的格式 // 默认的日期格式是 "Y-m-d H:i:s" $dateFormat = "Y n j, g:i a"; // 输出格式 $output = "%datetime% > %level_name% > %message% %context% %extra% "; $formatter = new LineFormatter($output, $dateFormat); $stream->setFormatter($formatter); // 把处理器放到对象里面 $this->handles $logger->pushHandler($stream); // 现在你就可以用日志服务了 $logger->info('My logger is now ready',['hello']); // 日志格式 2020 9 25, 9:43 pm > INFO > My logger is now ready ["hello"] {"name":"brady","dummy":"Hello world!"} 3 其他日志格式 按日期 生成 $logger->pushHandler(new MonologHandlerRotatingFileHandler(__DIR__."/log/hello.log",Logger::DEBUG)); //这个会按天产生日志 hello-2020-09-01.log [2020-09-25T21:43:34.047016+08:00] my_logger.INFO: My logger is now ready ["hello"] {"name":"brady","dummy":"Hello world!"} logstash格式 $stream1 = new StreamHandler(__DIR__.'/log/world.log', Logger::DEBUG); $logstashformatter = new MonologFormatterLogstashFormatter("ml","system","test","world"); $stream1->setFormatter($logstashformatter); $logger->pushHandler($stream1); {"@timestamp":"2020-09-25T21:43:34.047016+08:00","@version":1,"host":"system","message":"My logger is now ready","type":"ml","channel":"my_logger","level":"INFO","monolog_level":200,"test":{"name":"brady","dummy":"Hello world!"},"world":["hello"]} json格式 $stream2 = new MonologHandlerRotatingFileHandler("./log/wang.log"); $json = new MonologFormatterJsonFormatter(); $stream2->setFormatter($json); $logger->pushHandler($stream2); {"message":"My logger is now ready","context":["hello"],"level":200,"level_name":"INFO","channel":"my_logger","extra":{"name":"brady","dummy":"Hello world!"},"traceId":"trrrrrrrrr","timestamp":"2020-09-25T21:32:47.958836+0800"}ArrayArrayArray{"message":"My logger is now ready","context":["hello"],"level":200,"level_name":"INFO","channel":"my_logger","datetime":"2020-09-25T21:47:27.521873+08:00","extra":{"name":"brady","dummy":"Hello world!"},"traceId":"trrrrrrrrr"} <?php require "./vendor/autoload.php"; error_reporting(E_ALL); ini_set('display_errors', '1'); require __DIR__.'/vendor/autoload.php'; use MonologFormatterLineFormatter; use MonologHandlerFirePHPHandler; use MonologLogger; use MonologHandlerStreamHandler; // 创建日志服务 $logger = new Logger('my_logger'); // 定义一个handle $stream = new StreamHandler(__DIR__.'/log/wei.log', Logger::DEBUG); // 默认的日期格式是 "Y-m-d H:i:s" $dateFormat = "Y n j, g:i a"; // 输出格式 $output = "%datetime% > %level_name% > %message% %context% %extra% "; $formatter = new LineFormatter($output, $dateFormat); $stream->setFormatter($formatter); // 把处理器放到对象里面 $this->handles $logger->pushHandler($stream); //第二个处理器 传到hello.log 里面 $logger->pushHandler(new MonologHandlerRotatingFileHandler(__DIR__."/log/hello.log",Logger::DEBUG)); // 处理器 额外的数据给 $logger->pushProcessor(function ($record) { $record['extra']['dummy'] = 'Hello world!'; return $record; }); // 第三个处理器 $stream1 = new StreamHandler(__DIR__.'/log/world.log', Logger::DEBUG); $logstashformatter = new MonologFormatterLogstashFormatter("ml","system","test","world"); $stream1->setFormatter($logstashformatter); $logger->pushHandler($stream1); //第四个处理器 $stream2 = new MonologHandlerRotatingFileHandler("./log/wang.log"); $json = new MonologFormatterJsonFormatter(); $stream2->setFormatter($json); $logger->pushHandler($stream2); // 处理器 额外的数据给 $logger->pushProcessor(function ($record) { $record['extra']['name'] = 'brady'; $record['traceId'] = "trrrrrrrrr"; // 好像不行 只能是extra里面 因为record没这个字段 return $record; }); // 现在你就可以用日志服务了 $logger->info('My logger is now ready',['hello']);