You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
70 lines
1.9 KiB
70 lines
1.9 KiB
<?php
|
|
declare (strict_types=1);
|
|
|
|
namespace app\middleware;
|
|
|
|
use think\facade\Log;
|
|
use think\Request;
|
|
|
|
class DetailLog
|
|
{
|
|
|
|
/**
|
|
* 处理请求
|
|
* @param \think\Request $request
|
|
* @param \Closure $next
|
|
* @param string $name auth--必须有openid user--必须有user_id
|
|
* @date 2021-01-13
|
|
*/
|
|
public function handle($request, \Closure $next)
|
|
{
|
|
|
|
$requestInfo = [
|
|
'ip' => $request->ip(),
|
|
'method' => $request->method(),
|
|
'host' => $request->host(),
|
|
'uri' => $request->url(),
|
|
];
|
|
$logInfo = [
|
|
"{$requestInfo['ip']} {$requestInfo['method']} {$requestInfo['host']}{$requestInfo['uri']}",
|
|
'[ ROUTE ] ' . var_export($this->getRouteInfo(), true),
|
|
'[ HEADER ] ' . var_export($request->header(), true),
|
|
'[ PARAM ] ' . var_export($request->param(), true)
|
|
];
|
|
|
|
$logInfo = implode(PHP_EOL, $logInfo) . PHP_EOL;
|
|
Log::record($logInfo, 'info');
|
|
|
|
$response = $next($request);
|
|
|
|
if(request()->isPost()){
|
|
$content = $response->getContent();
|
|
$responseInfo = [
|
|
'[ RETURN ] ' . print_r($content,true),
|
|
];
|
|
|
|
$responseInfo = implode(PHP_EOL, $responseInfo) . PHP_EOL;
|
|
Log::record($responseInfo, 'info');
|
|
}
|
|
|
|
|
|
Log::record('---------------------------------------------------------------------------------------------------------------' . PHP_EOL, 'end');
|
|
return $response;
|
|
}
|
|
|
|
/**
|
|
* 获取路由信息
|
|
* @return array
|
|
*/
|
|
protected function getRouteInfo(): array
|
|
{
|
|
$request = app(Request::class);
|
|
|
|
return [
|
|
'rule' => $request->rule()->getRule(),
|
|
'route' => $request->rule()->getRoute(),
|
|
'option' => $request->rule()->getOption(),
|
|
'var' => $request->rule()->getVars(),
|
|
];
|
|
}
|
|
} |