monolog php编写

最后更新于:2022-04-02 02:24:26

[TOC] > [github](https://github.com/Seldaek/monolog) ## 安装 `composer require monolog/monolog` ## 使用 ``` date_default_timezone_set('RPC'); use Monolog\Formatter\LineFormatter; use Monolog\Logger; use Monolog\Handler\StreamHandler; use Monolog\Handler\FirePHPHandler; $dateFormat = "Y-m-d H:i:s"; // 默认的输出格式是 "[%datetime%] %channel%.%level_name%: %message% %context% %extra%\n" $output = "[%datetime%] %channel%.%level_name%: %message% %context% %extra%\n"; // 最后创建一个格式化器 $formatter = new LineFormatter($output, $dateFormat); // 创建一些处理器 $stream = new StreamHandler(__DIR__.'/my_app.log', Logger::DEBUG); $firephp = new FirePHPHandler(); $stream->setFormatter($formatter); // 创建应用的主要日志服务实例 $logger = new Logger('my_logger'); $logger->pushHandler($stream); $logger->pushHandler($firephp); /*设置扩展 %extra% 的值*/ $logger->pushProcessor(function ($record) { $record['extra']['dummy'] = 'Hello world!'; return $record; }); $logger->info("this is a info",['name'=>'asd']); //[2019 10 17, 2:31 am] my_logger.INFO: this is a info {"name":"asd"} {"dummy":"Hello world!"} // 使用另外的通道来创建安全相关的日志服务示例 $securityLogger = new Logger('security'); $securityLogger->pushHandler($stream); $securityLogger->pushHandler($firephp); // 或者克隆第一个,只是改变下通道 $securityLogger = $logger->withName('security'); $securityLogger->info("this is a security"); //[2019 10 17, 2:31 am] security.INFO: this is a security [] {"dummy":"Hello world!"} ```
';