0003-error.patch 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. From 4926fad38dec3cdc769af3873bc38a3409e327f8 Mon Sep 17 00:00:00 2001
  2. From: Martin <1808402272@qq.com>
  3. Date: Tue, 19 Oct 2021 16:00:00 +0800
  4. Subject: [PATCH 03/13] =?UTF-8?q?=E5=AE=8C=E5=96=84error=E6=97=A5=E5=BF=97?=
  5. =?UTF-8?q?=E5=86=85=E5=AE=B9?=
  6. MIME-Version: 1.0
  7. Content-Type: text/plain; charset=UTF-8
  8. Content-Transfer-Encoding: 8bit
  9. ---
  10. .env | 2 +-
  11. config/log.php | 4 +++-
  12. cores/ExceptionHandle.php | 30 +++++++++++++++++++++++++++---
  13. 3 files changed, 31 insertions(+), 5 deletions(-)
  14. diff --git a/.env b/.env
  15. index 55cf6c2..2042af2 100644
  16. --- a/.env
  17. +++ b/.env
  18. @@ -1 +1 @@
  19. -APP_DEBUG = true
  20. [APP]
  21. DEFAULT_TIMEZONE = Asia/Shanghai
  22. [DATABASE]
  23. TYPE = mysql
  24. HOSTNAME = 127.0.0.1
  25. DATABASE = yoshop2.0-feel
  26. USERNAME = root
  27. PASSWORD = root
  28. HOSTPORT = 3306
  29. CHARSET = utf8
  30. DEBUG = false
  31. [LANG]
  32. default_lang = zh-cn
  33. \ No newline at end of file
  34. +APP_DEBUG = false
  35. [APP]
  36. DEFAULT_TIMEZONE = Asia/Shanghai
  37. [DATABASE]
  38. TYPE = mysql
  39. HOSTNAME = 127.0.0.1
  40. DATABASE = yoshop2.0-feel
  41. USERNAME = root
  42. PASSWORD = root
  43. HOSTPORT = 3306
  44. CHARSET = utf8
  45. DEBUG = false
  46. [LANG]
  47. default_lang = zh-cn
  48. \ No newline at end of file
  49. diff --git a/config/log.php b/config/log.php
  50. index f8c8d9d..b4d9a4b 100644
  51. --- a/config/log.php
  52. +++ b/config/log.php
  53. @@ -34,8 +34,10 @@ return [
  54. 'processor' => null,
  55. // 关闭通道日志写入
  56. 'close' => false,
  57. + // 日志时间格式
  58. + 'time_format' => 'Y-m-d H:i:s',
  59. // 日志输出格式化
  60. - 'format' => '[%s][%s] %s',
  61. + 'format' => '[%s] [%s] %s',
  62. // 是否实时写入
  63. 'realtime_write' => false,
  64. ],
  65. diff --git a/cores/ExceptionHandle.php b/cores/ExceptionHandle.php
  66. index 2d25b1f..434f3dd 100644
  67. --- a/cores/ExceptionHandle.php
  68. +++ b/cores/ExceptionHandle.php
  69. @@ -13,7 +13,9 @@ namespace cores;
  70. use Throwable;
  71. use think\Response;
  72. use think\facade\Log;
  73. +use think\facade\Request;
  74. use think\exception\Handle;
  75. +use think\db\exception\PDOException;
  76. use app\common\exception\BaseException;
  77. /**
  78. @@ -105,17 +107,39 @@ class ExceptionHandle extends Handle
  79. */
  80. private function recordErrorLog(Throwable $e)
  81. {
  82. - // 生成日志内容
  83. + // 错误信息
  84. $data = [
  85. 'file' => $e->getFile(),
  86. 'line' => $e->getLine(),
  87. 'message' => $this->getMessage($e),
  88. 'status' => $this->getCode($e),
  89. ];
  90. - $log = "[{$data['status']}]{$data['message']} [{$data['file']}:{$data['line']}]";
  91. + // 日志内容
  92. + $log = '';
  93. + $log .= $this->getVisitor();
  94. + $log .= "\r\n" . "[ message ] [{$data['status']}] {$data['message']}";
  95. + $log .= "\r\n" . "[ file ] {$data['file']}:{$data['line']}";
  96. + // $log .= "\r\n" . "[ time ] " . format_time(time());
  97. + $log .= "\r\n" . '[ header ] ' . print_r(Request::header(), true);
  98. + $log .= "" . '[ param ] ' . print_r(Request::param(), true);
  99. + // 如果是数据库报错, 则记录sql语句
  100. + if ($e instanceof PDOException) {
  101. + $log .= "[ Error SQL ] " . $e->getData()['Database Status']['Error SQL'];
  102. + $log .= "\r\n";
  103. + }
  104. $log .= "\r\n" . $e->getTraceAsString();
  105. -
  106. + $log .= "\r\n" . '--------------------------------------------------------------------------------------------';
  107. // 写入日志文件
  108. Log::record($log, 'error');
  109. }
  110. +
  111. + /**
  112. + * 获取请求路径信息
  113. + * @return string
  114. + */
  115. + private function getVisitor()
  116. + {
  117. + $data = [Request::ip(), Request::method(), Request::url(true)];
  118. + return implode(' ', $data);
  119. + }
  120. }
  121. --
  122. 2.28.0.windows.1