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' => [