From ea70aafd30f1477c98ad45cf06582aa6634fc28b Mon Sep 17 00:00:00 2001 From: leekay0218 <272112456@qq.com> Date: Fri, 1 Apr 2022 14:27:45 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E7=A8=8B=E5=BA=8F=E7=9B=AE=E5=BD=95?= =?UTF-8?q?=E3=80=91=E4=BF=AE=E6=94=B9=E4=BA=86=E6=97=A5=E5=BF=97=E9=85=8D?= =?UTF-8?q?=E7=BD=AE=EF=BC=9A=E5=88=A0=E9=99=A4=E4=BA=86=E6=97=A0=E7=94=A8?= =?UTF-8?q?=E7=9A=84close=E9=85=8D=E7=BD=AE=EF=BC=8C=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=E4=BA=86=E4=B8=9A=E5=8A=A1=E6=88=90=E5=8A=9F=E5=92=8C=E5=A4=B1?= =?UTF-8?q?=E8=B4=A5=E7=9A=84=E5=BC=80=E5=85=B3=E3=80=82adminapi=EF=BC=8Ca?= =?UTF-8?q?pi=EF=BC=8Ckefuapi=E7=9A=84=E5=BC=82=E5=B8=B8=E5=A4=84=E7=90=86?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=BA=86=E6=97=A5=E5=BF=97=E8=BF=87=E6=BB=A4?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app/adminapi/AdminApiExceptionHandle.php | 49 +++++++++++------- crmeb/app/api/ApiExceptionHandle.php | 50 +++++++++++------- crmeb/app/event.php | 2 +- crmeb/app/kefuapi/KefuApiExceptionHandle.php | 51 ++++++++++++------- crmeb/app/listener/http/HttpEnd.php | 4 +- crmeb/config/log.php | 10 ++-- 6 files changed, 101 insertions(+), 65 deletions(-) diff --git a/crmeb/app/adminapi/AdminApiExceptionHandle.php b/crmeb/app/adminapi/AdminApiExceptionHandle.php index a70794c7b..80f2ca2db 100644 --- a/crmeb/app/adminapi/AdminApiExceptionHandle.php +++ b/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"); + } } /** diff --git a/crmeb/app/api/ApiExceptionHandle.php b/crmeb/app/api/ApiExceptionHandle.php index 034199da0..baeba135f 100644 --- a/crmeb/app/api/ApiExceptionHandle.php +++ b/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"); + } } /** diff --git a/crmeb/app/event.php b/crmeb/app/event.php index 0a54a6687..345159ebe 100644 --- a/crmeb/app/event.php +++ b/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控制器 diff --git a/crmeb/app/kefuapi/KefuApiExceptionHandle.php b/crmeb/app/kefuapi/KefuApiExceptionHandle.php index 6db923c0d..1e3580628 100644 --- a/crmeb/app/kefuapi/KefuApiExceptionHandle.php +++ b/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"); + } } /** diff --git a/crmeb/app/listener/http/HttpEnd.php b/crmeb/app/listener/http/HttpEnd.php index c55f582f9..250f76c45 100644 --- a/crmeb/app/listener/http/HttpEnd.php +++ b/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"; } diff --git a/crmeb/config/log.php b/crmeb/config/log.php index a30d00ad4..5cb08152e 100644 --- a/crmeb/config/log.php +++ b/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' => [