setRouter($request->path()); // 设置用户ID(如果已登录) if ($user = $request->user('admin')) { $logger->setUserId($user->id); } try { $response = $next($request); // 记录响应 if (method_exists($response, 'getContent')) { $content = $response->getContent(); if (is_string($content) && is_json($content)) { $logger->setJsonResponse($content); } } } catch (\Exception $e) { // 记录错误 $logger->setError($e->getMessage()); throw $e; } finally { // 记录运行时间 $logger->setRunTime($startTime); } return $response; } } if (!function_exists('is_json')) { function is_json($string) { json_decode($string); return json_last_error() === JSON_ERROR_NONE; } }