1515use crmeb \exceptions \AdminException ;
1616use crmeb \exceptions \ApiException ;
1717use crmeb \exceptions \AuthException ;
18- use think \exception \DbException ;
18+ use think \db \ exception \DbException ;
1919use think \exception \Handle ;
2020use think \exception \ValidateException ;
2121use think \facade \Env ;
22+ use think \facade \Log ;
2223use think \Response ;
2324use Throwable ;
2425
2526class AdminApiExceptionHandle extends Handle
2627{
28+ /**
29+ * 不需要记录信息(日志)的异常类列表
30+ * @var array
31+ */
32+ protected $ ignoreReport = [
33+ ValidateException::class,
34+ AuthException::class,
35+ AdminException::class,
36+ ApiException::class,
37+ ];
2738
2839 /**
2940 * 记录异常信息(包括日志或者其它方式记录)
30- *
3141 * @access public
3242 * @param Throwable $exception
3343 * @return void
3444 */
3545 public function report (Throwable $ exception ): void
3646 {
37- // 使用内置的方式记录异常日志
38- parent ::report ($ exception );
47+ if (!$ this ->isIgnoreReport ($ exception )) {
48+ $ data = [
49+ 'file ' => $ exception ->getFile (),
50+ 'line ' => $ exception ->getLine (),
51+ 'message ' => $ this ->getMessage ($ exception ),
52+ 'code ' => $ this ->getCode ($ exception ),
53+ ];
54+
55+ //日志内容
56+ $ log = [
57+ request ()->adminId (), //管理员ID
58+ request ()->ip (), //客户ip
59+ ceil (msectime () - (request ()->time (true ) * 1000 )), //耗时(毫秒)
60+ request ()->rule ()->getMethod (), //请求类型
61+ str_replace ("/ " , "" , request ()->rootUrl ()), //应用
62+ request ()->baseUrl (), //路由
63+ json_encode (request ()->param (), JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES ), //请求参数
64+ json_encode ($ data , JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES ), //报错数据
65+
66+ ];
67+ Log::write (implode ("| " , $ log ), "error " );
68+ }
3969 }
4070
4171 /**
4272 * Render an exception into an HTTP response.
43- *
4473 * @access public
4574 * @param \think\Request $request
4675 * @param Throwable $e
@@ -58,7 +87,7 @@ public function render($request, Throwable $e): Response
5887 if ($ e instanceof DbException) {
5988 return app ('json ' )->fail ('数据获取失败 ' , $ massageData );
6089 } elseif ($ e instanceof AuthException || $ e instanceof ValidateException || $ e instanceof ApiException) {
61- return app ('json ' )->make ($ e ->getCode () ?: 400 , $ e ->getMessage ());
90+ return app ('json ' )->make ($ e ->getCode () ? : 400 , $ e ->getMessage ());
6291 } elseif ($ e instanceof AdminException) {
6392 return app ('json ' )->fail ($ e ->getMessage (), $ massageData );
6493 } else {
0 commit comments