Sfoglia il codice sorgente

【程序目录】修改了日志配置:删除了无用的close配置,修改了业务成功和失败的开关。adminapi,api,kefuapi的异常处理增加了日志过滤。

leekay0218 3 anni fa
parent
commit
ea70aafd30

+ 30 - 19
crmeb/app/adminapi/AdminApiExceptionHandle.php

@@ -15,7 +15,6 @@ namespace app\adminapi;
 use crmeb\exceptions\AdminException;
 use crmeb\exceptions\ApiException;
 use crmeb\exceptions\AuthException;
-use think\exception\DbException;
 use think\exception\Handle;
 use think\exception\ValidateException;
 use think\facade\Env;
@@ -25,6 +24,16 @@ use Throwable;
 
 class AdminApiExceptionHandle extends Handle
 {
+    /**
+     * 不需要记录信息(日志)的异常类列表
+     * @var array
+     */
+    protected $ignoreReport = [
+        ValidateException::class,
+        AuthException::class,
+        AdminException::class,
+        ApiException::class,
+    ];
 
     /**
      * 记录异常信息(包括日志或者其它方式记录)
@@ -34,26 +43,28 @@ class AdminApiExceptionHandle extends Handle
      */
     public function report(Throwable $exception):void
     {
-        $data = [
-            'file' => $exception->getFile(),
-            'line' => $exception->getLine(),
-            'message' => $this->getMessage($exception),
-            'code' => $this->getCode($exception),
-        ];
+        if (!$this->isIgnoreReport($exception)) {
+            $data = [
+                'file' => $exception->getFile(),
+                'line' => $exception->getLine(),
+                'message' => $this->getMessage($exception),
+                'code' => $this->getCode($exception),
+            ];
 
-        //日志内容
-        $log = [
-            request()->adminId(),                                                                 //管理员ID
-            request()->ip(),                                                                      //客户ip
-            ceil(msectime() - (request()->time(true) * 1000)),                                    //耗时(毫秒)
-            request()->rule()->getMethod(),                                                       //请求类型
-            str_replace("/", "", request()->rootUrl()),                                           //应用
-            request()->baseUrl(),                                                                 //路由
-            json_encode(request()->param(), JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES),     //请求参数
-            json_encode($data, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES),                  //报错数据
+            //日志内容
+            $log = [
+                request()->adminId(),                                                                 //管理员ID
+                request()->ip(),                                                                      //客户ip
+                ceil(msectime() - (request()->time(true) * 1000)),                                    //耗时(毫秒)
+                request()->rule()->getMethod(),                                                       //请求类型
+                str_replace("/", "", request()->rootUrl()),                                           //应用
+                request()->baseUrl(),                                                                 //路由
+                json_encode(request()->param(), JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES),     //请求参数
+                json_encode($data, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES),                  //报错数据
 
-        ];
-        Log::write(implode("|", $log), "error");
+            ];
+            Log::write(implode("|", $log), "error");
+        }
     }
 
     /**

+ 31 - 19
crmeb/app/api/ApiExceptionHandle.php

@@ -12,9 +12,9 @@
 namespace app\api;
 
 
+use crmeb\exceptions\AdminException;
 use crmeb\exceptions\ApiException;
 use crmeb\exceptions\AuthException;
-use think\exception\DbException;
 use think\exception\Handle;
 use think\facade\Env;
 use think\facade\Log;
@@ -24,6 +24,16 @@ use think\exception\ValidateException;
 
 class ApiExceptionHandle extends Handle
 {
+    /**
+     * 不需要记录信息(日志)的异常类列表
+     * @var array
+     */
+    protected $ignoreReport = [
+        ValidateException::class,
+        AuthException::class,
+        AdminException::class,
+        ApiException::class,
+    ];
 
     /**
      * 记录异常信息(包括日志或者其它方式记录)
@@ -33,26 +43,28 @@ class ApiExceptionHandle extends Handle
      */
     public function report(Throwable $exception):void
     {
-        $data = [
-            'file' => $exception->getFile(),
-            'line' => $exception->getLine(),
-            'message' => $this->getMessage($exception),
-            'code' => $this->getCode($exception),
-        ];
+        if (!$this->isIgnoreReport($exception)) {
+            $data = [
+                'file' => $exception->getFile(),
+                'line' => $exception->getLine(),
+                'message' => $this->getMessage($exception),
+                'code' => $this->getCode($exception),
+            ];
 
-        //日志内容
-        $log = [
-            request()->uid(),                                                                     //用户ID
-            request()->ip(),                                                                      //客户ip
-            ceil(msectime() - (request()->time(true) * 1000)),                                    //耗时(毫秒)
-            request()->rule()->getMethod(),                                                       //请求类型
-            str_replace("/", "", request()->rootUrl()),                                           //应用
-            request()->baseUrl(),                                                                 //路由
-            json_encode(request()->param(), JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES),     //请求参数
-            json_encode($data, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES),                  //报错数据
+            //日志内容
+            $log = [
+                request()->uid(),                                                                     //用户ID
+                request()->ip(),                                                                      //客户ip
+                ceil(msectime() - (request()->time(true) * 1000)),                                    //耗时(毫秒)
+                request()->rule()->getMethod(),                                                       //请求类型
+                str_replace("/", "", request()->rootUrl()),                                           //应用
+                request()->baseUrl(),                                                                 //路由
+                json_encode(request()->param(), JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES),     //请求参数
+                json_encode($data, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES),                  //报错数据
 
-        ];
-        Log::write(implode("|", $log), "error");
+            ];
+            Log::write(implode("|", $log), "error");
+        }
     }
 
     /**

+ 1 - 1
crmeb/app/event.php

@@ -19,7 +19,7 @@ return [
     'listen' => [
         'AppInit' => [],
         'HttpRun' => [],
-        'HttpEnd' => [\app\listener\http\HttpEnd::class],
+        'HttpEnd' => [\app\listener\http\HttpEnd::class], //HTTP请求结束回调事件
         'LogLevel' => [],
         'LogWrite' => [],
         'StoreProductOrderDeliveryAfter' => [], // OrderSubscribe 送货 发送模板消息 admin模块 order.StoreOrder控制器/order.combinationOrder控制器

+ 33 - 18
crmeb/app/kefuapi/KefuApiExceptionHandle.php

@@ -12,6 +12,8 @@
 namespace app\kefuapi;
 
 
+use crmeb\exceptions\AdminException;
+use crmeb\exceptions\ApiException;
 use crmeb\exceptions\AuthException;
 use think\exception\Handle;
 use think\exception\ValidateException;
@@ -22,6 +24,17 @@ use Throwable;
 
 class KefuApiExceptionHandle extends Handle
 {
+    /**
+     * 不需要记录信息(日志)的异常类列表
+     * @var array
+     */
+    protected $ignoreReport = [
+        ValidateException::class,
+        AuthException::class,
+        AdminException::class,
+        ApiException::class,
+    ];
+
     /**
      * 记录异常信息(包括日志或者其它方式记录)
      * @access public
@@ -30,26 +43,28 @@ class KefuApiExceptionHandle extends Handle
      */
     public function report(Throwable $exception):void
     {
-        $data = [
-            'file' => $exception->getFile(),
-            'line' => $exception->getLine(),
-            'message' => $this->getMessage($exception),
-            'code' => $this->getCode($exception),
-        ];
+        if (!$this->isIgnoreReport($exception)) {
+            $data = [
+                'file' => $exception->getFile(),
+                'line' => $exception->getLine(),
+                'message' => $this->getMessage($exception),
+                'code' => $this->getCode($exception),
+            ];
 
-        //日志内容
-        $log = [
-            request()->kefuId(),                                                                  //客服ID
-            request()->ip(),                                                                      //客户ip
-            ceil(msectime() - (request()->time(true) * 1000)),                                    //耗时(毫秒)
-            request()->rule()->getMethod(),                                                       //请求类型
-            str_replace("/", "", request()->rootUrl()),                                           //应用
-            request()->baseUrl(),                                                                 //路由
-            json_encode(request()->param(), JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES),     //请求参数
-            json_encode($data, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES),                  //报错数据
+            //日志内容
+            $log = [
+                request()->kefuId(),                                                                  //客服ID
+                request()->ip(),                                                                      //客户ip
+                ceil(msectime() - (request()->time(true) * 1000)),                                    //耗时(毫秒)
+                request()->rule()->getMethod(),                                                       //请求类型
+                str_replace("/", "", request()->rootUrl()),                                           //应用
+                request()->baseUrl(),                                                                 //路由
+                json_encode(request()->param(), JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES),     //请求参数
+                json_encode($data, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES),                  //报错数据
 
-        ];
-        Log::write(implode("|", $log), "error");
+            ];
+            Log::write(implode("|", $log), "error");
+        }
     }
 
     /**

+ 2 - 2
crmeb/app/listener/http/HttpEnd.php

@@ -27,11 +27,11 @@ class HttpEnd
         $status = isset($response->getData()["status"]) ? $response->getData()["status"] : 0;
         if ($status == 200) {
             //业务成功日志开关
-            if (config("log.success_close")) return;
+            if (!config("log.success_log")) return;
             $type = "success";
         } else {
             //业务失败日志开关
-            if (config("log.fail_close")) return;
+            if (!config("log.fail_log")) return;
             $type = "fail";
         }
 

+ 4 - 6
crmeb/config/log.php

@@ -20,12 +20,10 @@ return [
     'level' => ['error', 'warning', 'fail', 'success'],
     // 日志类型记录的通道 ['error'=>'email',...]
     'type_channel' => [],
-    // 是否关闭日志写入
-    'close' => false,
-    //是否关闭业务成功日志
-    'success_close' => false,
-    //是否关闭业务失败日志
-    'fail_close' => false,
+    //是否开启业务成功日志
+    'success_log' => false,
+    //是否开启业务失败日志
+    'fail_log' => false,
     // 日志通道列表
     'channels' => [
         'file' => [