ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
## 为每个请求添加日志 - 方便查找错误信息 - 操作:增加两个中间件:一个是请求日志,一个是反馈日志 - 适用于 `laravel` 和 `lumen` 请求日志中间件 app\\Http\\Middleware\\RequestLog.php ~~~ <?php namespace App\Http\Middleware; use Closure; use Illuminate\Support\Facades\Log; class RequestLog { // describe: 记录请求日志 public function handle($request, Closure $next) { try{ Log::debug('Request | '.$request->getPathInfo().' |', $request->input()); $response = $next($request); return $response; } catch (HttpException $e){ return $this->ajaxResponse($e->getStatusCode(), $e->getMessage(), (object)[]); } catch (\Exception $e){ return $this->apiSysErrorResponse($e->getMessage(), (object)[]); } } } ~~~ 响应日志中间件 `app\\Http\\Middleware\\ResponseLog.php` ~~~ <?php namespace App\Http\Middleware; use Closure; use Illuminate\Support\Facades\Log; class ResponseLog { // describe: 记录响应日志 public function handle($request, Closure $next) { try{ $cline = chr(10); $response = $next($request); $return = json_encode(json_decode($response->getContent(), 1), JSON_UNESCAPED_UNICODE).$cline; //Log::debug('Response | '.$request->getPathInfo().' | '.$response->getContent()); Log::debug('Response | '.$request->getPathInfo().' | '.$return); return $response; } catch (HttpException $e){ return $this->ajaxResponse($e->getStatusCode(), $e->getMessage(), (object)[]); } catch (\Exception $e){ return $this->apiSysErrorResponse($e->getMessage(), (object)[]); } } } ~~~