mathewparet / laravel-global-log-context
添加全局日志上下文
v1.0.1
2023-08-07 09:30 UTC
Requires
- illuminate/http: ^10.17
- illuminate/log: ^10.17
- illuminate/support: ^10.17
This package is auto-updated.
Last update: 2024-09-07 11:36:35 UTC
README
轻松将额外的日志上下文添加到所有日志中
安装
安装库
composer require mathewparet/laravel-global-log-context
创建上下文定义类
// app/tools/LogContext/LogContext.php
use mathewparet\LaravelGlobalLogContext\Contracts\ContextDefinition;
namespace App\Tools\LogContext;
class LogContext implements ContextDefinition
{
public static function context(): array
{
return [
'user' => request()?->user()?->email,
'ip' => requiest()?->ip()
];
}
}
将其绑定到您的服务提供者
// app/providers/AppServiceProvider.php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use mathewparet\LaravelGlobalLogContext\Contracts\ContextDefinition;
use App\Tools\LogContext\LogContext;
class AppServiceProvider extends ServiceProvider
{
public function register()
{
$this->app->bind(ContextDefinition::class, LogContext::class);
}
}
更新日志配置
// config/logging.php
// ...
use mathewparet\LaravelGlobalLogContext\ExtraLoggingContext\AddExtraContextToLogs;
return [
// ...
'channels' => [
'stack' => [
'driver' => 'stack',
// ....
'tap' => AddExtraContextToLogs::class
]
]
];