原文:https://gist.github.com/cesargb/d6ee27df4ce0c7e22ac7cc7c13daddba
0x01
Laravel 原有的日志记录文件只记录了时间和日志内容没有记录是在哪个文件记录的,无法用日志快速定位
0x02 创建provider
php artisan make:provider LogProcessorServiceProvider
0x03 填入以下内容
<?php
namespace App\Providers;
use App;
use Monolog\Processor\GitProcessor;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\ServiceProvider;
use Monolog\Processor\MemoryUsageProcessor;
use Monolog\Processor\IntrospectionProcessor;//此处与原文不同,我这边需要记录调用类信息
use Monolog\Logger;
class LogProcessorServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
$log = $this->app->get('log');
$log->pushProcessor(function ($record) {
$record['extra']['ip'] = request()->ip();
if (Auth::check()) {
$record['extra']['user_id'] = Auth::id();
}
return $record;
});
if (config('app.debug')) {
$log->pushProcessor(new MemoryUsageProcessor());
}
/**
* 在config/logging 文件中新增一个控制位 introspection 用于控制 是否在日志中显示调用类等信息
*/
if (config('logging.introspection')){
$log->pushProcessor(new IntrospectionProcessor(Logger::DEBUG,['Illuminate\\']);
}
}
/**
* Register any application services.
*
* @return void
*/
public function register()
{
}
}
0x03 在config/app.php注册provider
编辑app/config.php注册LogProcessorServiceProvider:
'providers' => [
// ...
App\Providers\LogProcessorServiceProvider::class,
];
0x04 其它Monolog 自带 Processor 参考:
https://github.com/Seldaek/monolog/blob/main/doc/02-handlers-formatters-processors.md#processors