AppLog.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2024 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace cores\middleware;
  13. use think\Response;
  14. use think\facade\Log as FacadeLog;
  15. use app\common\library\Log;
  16. /**
  17. * 中间件:应用日志
  18. */
  19. class AppLog
  20. {
  21. // 访问日志
  22. private static string $beginLog = '';
  23. /**
  24. * 前置中间件
  25. * @param \think\Request $request
  26. * @param \Closure $next
  27. * @return mixed
  28. */
  29. public function handle(\think\Request $request, \Closure $next)
  30. {
  31. // 记录访问日志
  32. if (env('begin_log')) {
  33. $log = $this->getVisitor($request);
  34. $log .= "\r\n" . '[ header ] ' . print_r($request->header(), true);
  35. $log .= "" . '[ param ] ' . print_r($request->param(), true);
  36. $log .= '--------------------------------------------------------------------------------------------';
  37. static::$beginLog = $log;
  38. }
  39. return $next($request);
  40. }
  41. /**
  42. * 记录访问日志
  43. * @param Response $response
  44. */
  45. public function end(Response $response)
  46. {
  47. FacadeLog::record(static::$beginLog, 'begin');
  48. Log::end();
  49. }
  50. /**
  51. * 获取请求路径信息
  52. * @param \think\Request $request
  53. * @return string
  54. */
  55. private function getVisitor(\think\Request $request): string
  56. {
  57. $data = [$request->ip(), $request->method(), $request->url(true)];
  58. return implode(' ', $data);
  59. }
  60. }