programic / laravel-http-logger
Laravel HTTP请求日志包
Requires
- php: ^7.4|^8.0
- illuminate/support: ^8.0|^9.0|^10.0|^11.0
Requires (Dev)
- orchestra/testbench: ^6.0|^7.0|^8.0|^9.0
- pestphp/pest: ^1.22|^2.34
- phpunit/phpunit: ^9.0|^10.5
README
此包添加了一个中间件,可以记录到默认日志中。如果用户的请求过程中出现问题,您仍然可以访问用户发送的原始请求数据。
此日志可以作为关键用户提交的额外安全网,例如生成潜在客户的表单。
安装
您可以通过composer安装此包
composer require programic/laravel-http-logger
可选地,您可以使用以下命令发布配置文件:
php artisan vendor:publish --provider="Programic\HttpLogger\HttpLoggerServiceProvider" --tag="config"
可选地,您可以使用以下命令发布迁移文件:
php artisan vendor:publish --provider="Programic\HttpLogger\HttpLoggerServiceProvider" --tag="migrations"
这是已发布配置文件的内容
return [ /* * The log profile which determines whether a request should be logged. * It should implement `LogProfile`. */ 'log_profile' => \Programic\HttpLogger\LogNonGetRequests::class, /* * The log writer used to write the request to a log. * It should implement `LogWriter`. */ 'log_writer' => \Programic\HttpLogger\DefaultLogWriter::class, /* * The log channel used to write the request. */ 'log_channel' => env('LOG_CHANNEL', 'stack'), /* * The log level used to log the request. */ 'log_level' => 'info', /* * Filter out body fields which will never be logged. */ 'except' => [ 'password', 'password_confirmation', ], /* * List of headers that will be sanitized. For example Authorization, Cookie, Set-Cookie... */ 'sanitize_headers' => [], ];
使用方法
此包提供了一种中间件,可以作为全局中间件或单个路由添加。
Laravel >= 11
->withMiddleware(function (Middleware $middleware) { $middleware->append(\Programic\HttpLogger\Middlewares\HttpLogger::class); })
Laravel <= 10
// in `app/Http/Kernel.php` protected $middleware = [ // ... \Programic\HttpLogger\Middlewares\HttpLogger::class ];
// in a routes file Route::post('/submit-form', function () { // })->middleware(\Programic\HttpLogger\Middlewares\HttpLogger::class);
日志记录
使用两个类来处理传入请求的日志:一个LogProfile
类将确定是否记录请求,而LogWriter
类将请求写入日志。
此包内添加了一个默认日志实现。它只会记录POST
、PUT
、PATCH
和DELETE
请求,并将写入默认Laravel日志。
您可以根据自己的需要实现自己的日志配置文件和/或日志写入类,并在config/http-logger.php
中进行配置。
自定义日志配置文件必须实现\Programic\HttpLogger\LogProfile
。此接口要求您实现shouldLogRequest
。
// Example implementation from `\Programic\HttpLogger\LogNonGetRequests` public function shouldLogRequest(Request $request): bool { return in_array(strtolower($request->method()), ['post', 'put', 'patch', 'delete']); }
自定义日志写入必须实现\Programic\HttpLogger\LogWriter
。此接口要求您实现logRequest
。
// Example implementation from `\Programic\HttpLogger\DefaultLogWriter` public function logRequest(Request $request): void { $method = strtoupper($request->getMethod()); $uri = $request->getPathInfo(); $bodyAsJson = json_encode($request->except(config('http-logger.except'))); $message = "{$method} {$uri} - {$bodyAsJson}"; Log::channel(config('http-logger.log_channel'))->info($message); }
隐藏敏感头信息
您可以在将它们发送到日志之前定义要清理的头信息。最常见的一个例子是Authorization头信息。如果您不想记录jwt令牌,可以将该头信息添加到http-logger.php
配置文件中
// in config/http-logger.php return [ // ... 'sanitize_headers' => [ 'Authorization' ], ];
输出将是Authorization: "****"
而不是Authorization: "Bearer {token}"
测试
composer test
更新日志
请参阅更新日志以获取更多最近更改的信息。
贡献
请参阅贡献指南以获取详细信息。
安全
如果您发现与安全相关的错误,请通过邮件security@programic.be联系,而不是使用问题跟踪器。
致谢
许可证
MIT许可证(MIT)。请参阅许可证文件以获取更多信息。