回答

收藏

springboot @ExceptionHandler 异常 取消默认日志处理6金钱.anslist{backgr

技术问答 技术问答 408 人阅读 | 0 人回复 | 2023-09-11


    ) j4 j5 C8 u7 _$ O- T( t
  • @Slf4j" t: j3 \3 u# U& F; R# d& R
  • @RestControllerAdvice% Z; y& B- l7 [7 p, M0 q. @
  • public class GlobalExceptionHandler {
    " `. G; D) F& x4 F8 d* P- N# D7 |

  • 1 }8 f, h' `% Y9 b
  •     @ResponseStatus(HttpStatus.BAD_REQUEST)  |+ G+ y/ E, F  D* B; b
  •     @ExceptionHandler(RuntimeException.class)8 L) x! ^4 M0 p, P1 e$ H. y
  •     public AjaxResult handler(RuntimeException e) {
    1 w: D% L2 ?* ]2 A& g4 ]
  •         // e.printStackTrace();- u/ ~3 e. b7 S4 g1 S
  •         log.error("Assert异常:----------{}",e.getMessage());4 _/ V4 J0 t. b4 p0 s
  •         return AjaxResult.fail(e.getMessage());+ _0 F5 f7 B" W8 C  v
  •     }7 I8 N! Z" `. A7 |8 Q
  •     @ResponseStatus(HttpStatus.BAD_REQUEST)5 [/ c' P! Z# ~0 }3 j
  •     @ExceptionHandler(IllegalArgumentException.class)
    : \( M' m9 ?# d9 A( g. r2 W4 y: U
  •     public AjaxResult handler(IllegalArgumentException e) {
    * _( }2 B4 f0 C0 L4 C- D9 z
  •         log.error("Assert异常:----------{}",e.getMessage());
    ; ?$ d% ]% a1 d; g* g- T  J
  •         return AjaxResult.fail(e.getMessage());3 c! d& i8 ?: X% T- B) U& ^
  •     }3 V0 c  F/ ]0 q& O1 Y" C6 V" n
  • }
    1 o7 X2 h% G- m/ [/ w# E
$ x3 p: ^0 r7 o/ Q1 v; o8 \& w5 }3 ~

    / R  t2 }# H0 `) n6 x2 ~9 B- c
  • @RestController
    & g' t% g* W) Z1 N4 T
  • public class TestController {
    2 R0 {$ p& ?9 }; z

  • , f& ]- s% d4 n# o" E6 ?
  •     @Autowired$ M& H% ~& ~  g! ~5 d. w* ~
  •     private SysUserService sysUserService;7 I$ l, w" ?5 y: D

  • / j6 E, K; S) t( w3 d
  •     @GetMapping("/test1")
    ! l; x) q! {3 Q3 T1 Q; ~
  •     public Object test1() {
    * ]  [" i& k4 `) X! E8 q( f  O6 r
  •         int a = 10/0;9 P" F3 y8 p7 |+ V
  •         return "1";; \' z$ v' A/ [8 ?1 E" o
  •     }
    8 ~! @0 X! [- i0 g! s1 s) k8 E

  • " D* |* A' H  @
  •     @GetMapping("/test2")5 z8 l- o& q) N" d. i" h( P; c
  •     public Object test2() {
    ! p, b  b( _$ ~5 [+ @
  •         return sysUserService.list();) M5 D7 m& m6 \5 I4 ]
  •     }7 K  Q1 t- u" e
  • }
    3 ^7 e' [6 w' P: E5 I; c; _
如图所示 访问 test1 资源会报 by zero 错误 控制台会打印出 2条log记录
8 R! E* L/ J) n) R# @

    3 M1 ^4 N  ]$ j9 o0 \6 {
  • error级别 log.error("Assert异常:----------{}",e.getMessage());warn级别 springboot 自带的log处理。; o- N% `! ?" d' u
有没有大佬研究过怎么取消 springboot的 默认warn 提示
9 E! B. z* c, q我知道答案 回答被采纳将会获得6 金钱 已有0人回答

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
分享到:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则