这个包增加了一个中间件,能够将传入的申请记录到默认日志中。如果在用户申请期间呈现任何问题,您依然能够拜访该用户发送的原始申请数据。
装置
您能够通过 composer 装置软件包:
composer require whereof/think-http-logger
配置文件 config/http-logger.php
内容:
<?php
return [
/*
* The log profile which determines whether a request should be logged.
* It should implement `LogProfile`.
*/
'log_profile' => \whereof\think\HttpLogger\LogNonGetRequests::class,
/*
* The log writer used to write the request to a log.
* It should implement `LogWriter`.
*/
'log_writer' => \whereof\think\HttpLogger\DefaultLogWriter::class,
/*
* The log channel used to write the request.
*/
'log_channel' => env('LOG_CHANNEL', 'file'),
];
控制器中应用
use whereof\think\HttpLogger\Middlewares\HttpLogger;
class Index extends BaseController
{
protected $middleware =[HttpLogger::class];
public function index()
{return 'test HttpLogger';}
}
路由中应用
Route::get('think', function () {return 'hello,ThinkPHP6!';})->middleware(\whereof\think\HttpLogger\Middlewares\HttpLogger::class);
全局app/middleware.php
<?php
// 全局中间件定义文件
return [
......
\whereof\think\HttpLogger\Middlewares\HttpLogger::class
];
日志记录
两个类用于解决传入申请的日志记录:LogProfile
类将确定是否应记录申请,LogWriter
类将申请写入日志。
在这个包中增加了一个默认的日志实现。它只会记录 POST
、PUT
、PATCH
和 DELETE
申请,并将写入默认的 thinkphp 记录器。
您能够自在实现本人的日志配置文件和 / 或日志编写器类,并在 config/http-logger.php
中进行配置。
自定义日志配置文件必须实现 \whereof\think\HttpLogger\LogProfile
。这个接口须要你实现 shouldLogRequest。
// Example implementation from `whereof\think\HttpLogger\LogNonGetRequests`
public function shouldLogRequest(Request $request): bool
{return in_array(strtolower($request->method()), ['post', 'put', 'patch', 'delete']);
}
自定义日志编写器必须实现 \whereof\think\HttpLogger\LogWriter。这个接口须要你实现 logRequest。
// Example implementation from ` \whereof\think\HttpLogger\DefaultLogWriter`
public function logRequest(Request $request): void
{$method = strtoupper($request->method());
$uri = $request->pathinfo();
$bodyAsJson = json_encode($request->all());
$message = "{$method} {$uri} - {$bodyAsJson}";
Log::channel(config('http-logger.log_channel'))->info($message);
}
License
The MIT License (MIT). Please see License File for more information.