kriss / webman-logger
Requires
- php: >=7.2
- monolog/monolog: ^2.6
Requires (Dev)
README
webman 日志统筹化管理插件
简介
webman 支持原始的 monolog 配置形式,配置灵活,但以下情况并不方便:
- 当日志特别多时,不可能所有日志都通过
Log::info
形式记录,肯定需要分 channel。 - 当 channel 特别多时:每个都需要单独定义,但大部分都是复制粘贴的,后期切换所有通道写入渠道时,需要逐一修改,不易维护。
- 每次通过
Log::channel('channelName')
形式调用,因为channelName
是字符串,万一拼错单词,会导致日志记录失败。 - 没有很好地利用 monolog 的 formatter 和 processor。
此插件旨在解决以上问题,针对多 channel
模式进行统筹优化管理。
安装
composer require webman-tech/logger
配置
-
主要的配置文件位于:
config/plugin/webman-tech/logger/log-channel.php
,按需调整,后文将对其中部分配置进行详细说明。 -
(建议)自定义一个继承自
WebmanTech\Logger\Logger
的 Logger 类,例如support\facade\Logger
,便于后期扩展和使用。 -
(必须)在
config/log.php
中合并原来的配置和Logger::getLogChannelConfigs()
,例如:
use support\facade\Logger; return array_merge( [ 'default' => [ 'handlers' => [ [ 'class' => Monolog\Handler\RotatingFileHandler::class, 'constructor' => [ runtime_path() . '/logs/webman.log', 7, //$maxFiles Monolog\Logger::DEBUG, ], 'formatter' => [ 'class' => Monolog\Formatter\LineFormatter::class, 'constructor' => [null, 'Y-m-d H:i:s', true], ], ] ], ], ], Logger::getLogChannelConfigs(), // merge 这个 );
-
新增一个日志 channel,执行以下两步操作:
- (必须)在
config/plugin/webman-tech/logger/log-channel.php
的channels
中添加日志 channel 的名字,建议小驼峰命名,例如purchaseOrder
。 - (建议)在
support\facade\Logger
的类上方添加注释:@method static void purchaseOrder($msg, string $type = 'info', array $context = [])
。
- (必须)在
步骤2是为了代码提示,后期记录日志可以直接使用 support\facade\Logger::purchaseOrder('xxx')
的形式。如果未定义该类,一样可以使用 support\Log::channel('purchaseOrder')->info('xxx')
的方式记录日志。
后期新增其他 channel,只需重复步骤4即可。
使用
假设已经在 config/plugin/webman-tech/logger/log-channel.php
的 channels
中配置了两个 channels: app 和 sql,建议有如下 Logger 类:
<?php namespace support\facade; /** * @method static void app($msg, string $type = 'info', array $context = []) * @method static void sql($msg, string $type = 'info', array $context = []) */ class Logger extends \WebmanTech\Logger\Logger { }
记录日志
use support\facade\Logger; Logger::app('xxx'); // 记录 xxx 到 app channel Logger::app(['x' => 'y']); // 支持数组写入,写入日志时会转成 json Logger::app($exception); // 支持 Exception 写入,写入时会记录详细的 trace Logger::app('xxx', 'error'); // 切换 level Logger::app('hello: {name}', 'info', ['name' => 'Kriss']); // 使用 PsrLogMessageProcessor 处理,会记录成 "hello Kriss"
模式(Mode)介绍
模式基本等同于 monolog 的 Handler,基本来说就是 Handler 的二次封装,多个模式可以同时启用,以下时已有的模式介绍
SplitMode
不同的channel会被记录到不同的目录下,目录名和文件名均为 channel 同名,如:
- runtime
- logs
- app
- app-2022-05-28.log
- app-2022-05-29.log
- sql
- sql-2022-05-28.log
- sql-2022-05-29.log
- app
- logs
此模式比较适合开发和测试,按 channel 和日期分开日志,方便排查错误。
MixMode
不同的channel会被记录到同一个目录下,目录名和文件名均为 channelMixed(可修改),如:
- runtime
- logs
- channelMixed
- channelMixed-2022-05-28.log
- channelMixed-2022-05-29.log
- channelMixed
- logs
此模式比较适合将日志文件写入到 elk 等其他集中日志管理的服务中,因为一般此类通过 agent 来收集日志的服务不太会兼容动态扩展的日志目录。
在 log 文件中如何区分 channel?在记录的日志中有一列是 [channelName]
StdoutMode
将所有日志输出到控制台
此模式适合 docker 环境
formatter 介绍
formatter 结构化可以有效地方便日志的筛查和查看
ChannelFormatter
基本的通道格式
单行日志格式如下:
[时间][请求的唯一标识][日志级别][客户端ip][当前登录的用户ID][路由path]: 日志内容
ChannelMixedFormatter
mix mode 使用的格式,比 ChannelFormatter 多了一列 channelName
单行日志格式如下:
[时间][请求的唯一标识][channelName][日志级别][客户端ip][当前登录的用户ID][路由path]: 日志内容