HttpLog.php 889 B

123456789101112131415161718192021222324252627282930313233343536
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\middleware;
  4. use think\facade\Log;
  5. class HttpLog
  6. {
  7. /**
  8. * 处理请求
  9. *
  10. * @param \think\Request $request
  11. * @param \Closure $next
  12. * @return Response
  13. */
  14. public function handle($request, \Closure $next)
  15. {
  16. if ($request->isOptions()) {
  17. error('OPTIONS请求');
  18. }
  19. $data=[
  20. 'url'=>array_values(array_filter(explode('/',$request->url()))),
  21. 'method'=>$request->method(),
  22. 'ip'=>$request->ip(),
  23. 'param'=>$request->param(),
  24. 'addtime'=>date('Y-m-d H:i:s')
  25. ];
  26. Log::info(json_encode($data,JSON_UNESCAPED_UNICODE));
  27. return $next($request);
  28. }
  29. public function end(\think\Response $response)
  30. {
  31. Log::info('响应结束:'.json_encode($response->getData(),JSON_UNESCAPED_UNICODE));
  32. }
  33. }