有时候在一些场景下,想看看日志写入的毫秒甚至微秒数,但是laravel的日志中记录的时间默认最小单位只到秒,如下示例:
1 2 3 |
for ($i=1; $i < 100; $i++) { Log::info($i); } |
1 2 3 4 5 6 7 8 9 10 11 |
[2022-12-09 15:49:29] local.INFO: 1 [2022-12-09 15:49:29] local.INFO: 2 [2022-12-09 15:49:29] local.INFO: 3 [2022-12-09 15:49:29] local.INFO: 4 [2022-12-09 15:49:29] local.INFO: 5 [2022-12-09 15:49:29] local.INFO: 6 [2022-12-09 15:49:29] local.INFO: 7 [2022-12-09 15:49:29] local.INFO: 8 [2022-12-09 15:49:29] local.INFO: 9 [2022-12-09 15:49:29] local.INFO: 10 ... |
如果要加上微秒只需要改动一下config/logging.php配置文件即可,我们以daily示例。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
'daily' => [ 'driver' => 'daily', 'path' => storage_path('logs/laravel.log'), 'level' => env('LOG_LEVEL', 'debug'), 'days' => 14, //日志里面的时间增加微妙 'formatter' => Monolog\Formatter\LineFormatter::class, 'formatter_with' => [ 'dateFormat' => 'Y-m-d H:i:s.u', //框架默认日志内容格式,如有需求在改动。 //'format' => "[%datetime%] %channel%.%level_name%: %message% %context% %extra%\n", //加上微秒之后,如果日志中不打印context和extra字段的话,默认会显示两个空白的大括号[],下面两个为true可以控制两个空白大括号默认不显示。 'allowInlineLineBreaks' => true, 'ignoreEmptyContextAndExtra' => true, ], ], |
根据上面示例,在daily里面加上formatter , formatter_with即可。 最后写出来的日志效果: [crayon-676858114cfc7217807739/…