|
|
|
|
@ -4,6 +4,7 @@
|
|
|
|
|
|
|
|
|
|
use Closure;
|
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
use Illuminate\Support\Facades\Log as LaravelLog;
|
|
|
|
|
use App\Services\LogService;
|
|
|
|
|
|
|
|
|
|
class Log
|
|
|
|
|
@ -19,38 +20,77 @@ public function handle(Request $request, Closure $next)
|
|
|
|
|
{
|
|
|
|
|
$insert_id=0;
|
|
|
|
|
$insert_id=self::requestLog($request,$insert_id); //记录请求时日志,不含返回信息
|
|
|
|
|
$response = $next($request);
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
$response = $next($request);
|
|
|
|
|
} catch (\Throwable $e) {
|
|
|
|
|
// 捕获控制器层的异常
|
|
|
|
|
LaravelLog::error('Log中间件捕获异常-请求处理失败', [
|
|
|
|
|
'error' => $e->getMessage(),
|
|
|
|
|
'file' => $e->getFile(),
|
|
|
|
|
'line' => $e->getLine(),
|
|
|
|
|
'url' => $request->getPathInfo(),
|
|
|
|
|
'method' => $request->method(),
|
|
|
|
|
'post_data' => $request->post(),
|
|
|
|
|
'get_data' => $request->query()
|
|
|
|
|
]);
|
|
|
|
|
throw $e; // 重新抛出异常,让Laravel异常处理器处理
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if($request->getPathInfo()=='/api/v1/mH5/GetPersonPdfDetailByLink'){
|
|
|
|
|
return $response;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$content = $response->getContent();
|
|
|
|
|
$data = json_decode($content, true); // 解码响应内容为关联数组
|
|
|
|
|
try {
|
|
|
|
|
$content = $response->getContent();
|
|
|
|
|
$data = json_decode($content, true); // 解码响应内容为关联数组
|
|
|
|
|
|
|
|
|
|
// 在关联数组中添加 code 字段
|
|
|
|
|
// $data['code'] = 200;
|
|
|
|
|
$data['code'] = $response->getStatusCode();
|
|
|
|
|
$modifiedContent = json_encode($data,JSON_UNESCAPED_UNICODE); // 编码修改后的关联数组为 JSON 字符串
|
|
|
|
|
$response->setContent($modifiedContent);
|
|
|
|
|
// 检查JSON解码是否成功
|
|
|
|
|
if ($data === null && json_last_error() !== JSON_ERROR_NONE) {
|
|
|
|
|
LaravelLog::error('Log中间件-响应内容JSON解码失败', [
|
|
|
|
|
'url' => $request->getPathInfo(),
|
|
|
|
|
'json_error' => json_last_error_msg(),
|
|
|
|
|
'content_preview' => substr($content, 0, 500),
|
|
|
|
|
'status_code' => $response->getStatusCode()
|
|
|
|
|
]);
|
|
|
|
|
return $response; // 直接返回原始响应,不处理
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(env('REQUEST_LOG') and $response->getStatusCode()==200){ //如果返回状态为200进行log
|
|
|
|
|
// 在关联数组中添加 code 字段
|
|
|
|
|
// $data['code'] = 200;
|
|
|
|
|
$data['code'] = $response->getStatusCode();
|
|
|
|
|
$modifiedContent = json_encode($data,JSON_UNESCAPED_UNICODE); // 编码修改后的关联数组为 JSON 字符串
|
|
|
|
|
$response->setContent($modifiedContent);
|
|
|
|
|
|
|
|
|
|
$ip=self::getTrustedProxiesIp(); //真实ip
|
|
|
|
|
$request_header=$request->header(); //请求头
|
|
|
|
|
//dd($response);
|
|
|
|
|
$response_data = $response->getData(); //返回data,json格式
|
|
|
|
|
$post_data=$request->post(); //post请求数据
|
|
|
|
|
$get_data=$request->query(); //get请求
|
|
|
|
|
$request_url=$request->getPathInfo();//访问的接口地址
|
|
|
|
|
$log=app()->make(LogService::class);
|
|
|
|
|
$log->RequestLog([
|
|
|
|
|
'ip'=>$ip,
|
|
|
|
|
'response_data'=>$response_data,
|
|
|
|
|
'request_header'=>$request_header,
|
|
|
|
|
'post_data'=>$post_data,
|
|
|
|
|
'get_data'=>$get_data,
|
|
|
|
|
'request_url'=>$request_url,
|
|
|
|
|
],$insert_id);
|
|
|
|
|
if(env('REQUEST_LOG') and $response->getStatusCode()==200){ //如果返回状态为200进行log
|
|
|
|
|
|
|
|
|
|
$ip=self::getTrustedProxiesIp(); //真实ip
|
|
|
|
|
$request_header=$request->header(); //请求头
|
|
|
|
|
//dd($response);
|
|
|
|
|
$response_data = $response->getData(); //返回data,json格式
|
|
|
|
|
$post_data=$request->post(); //post请求数据
|
|
|
|
|
$get_data=$request->query(); //get请求
|
|
|
|
|
$request_url=$request->getPathInfo();//访问的接口地址
|
|
|
|
|
$log=app()->make(LogService::class);
|
|
|
|
|
$log->RequestLog([
|
|
|
|
|
'ip'=>$ip,
|
|
|
|
|
'response_data'=>$response_data,
|
|
|
|
|
'request_header'=>$request_header,
|
|
|
|
|
'post_data'=>$post_data,
|
|
|
|
|
'get_data'=>$get_data,
|
|
|
|
|
'request_url'=>$request_url,
|
|
|
|
|
],$insert_id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} catch (\Throwable $e) {
|
|
|
|
|
// 捕获响应处理中的异常
|
|
|
|
|
LaravelLog::error('Log中间件-响应处理失败', [
|
|
|
|
|
'error' => $e->getMessage(),
|
|
|
|
|
'file' => $e->getFile(),
|
|
|
|
|
'line' => $e->getLine(),
|
|
|
|
|
'url' => $request->getPathInfo(),
|
|
|
|
|
'trace' => $e->getTraceAsString()
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|