1. log.error(“异常信息:”+e.getMessage)
没有异常信息,没有堆栈信息
@PostMapping("/logtest") public void logtest(){ try { int i=1/0; } catch (Exception e) { log.error("异常信息:"+e.getMessage()); } }
测试结果:
2024-03-13 14:12:16.555 | ERROR | http-nio-7012-exec-1 | com.ttbank.flep.controller.UserController:70 | [] | [] - 异常信息:/ by zero
2 log.error(“异常信息:”+e)
@PostMapping("/logtest") public void logtest(){ try { int i=1/0; } catch (Exception e) { log.error("异常信息:"+e); } }
测试结果:
2024-03-13 14:17:02.532 | ERROR | http-nio-7012-exec-1 | com.ttbank.flep.controller.UserController:70 | [] | [] - 异常信息:java.lang.ArithmeticException: / by zero
3 log.error(“异常信息:”,e);
最佳实践。可以得到异常的信息和堆栈信息
@PostMapping("/logtest") public void logtest(){ try { int i=1/0; } catch (Exception e) { log.error("异常信息:", e); } }
测试结果:
2024-03-13 14:22:17.725 | ERROR | http-nio-7012-exec-2 | com.ttbank.flep.controller.UserController:70 | [] | [] - 异常信息: java.lang.ArithmeticException: / by zero at com.ttbank.flep.controller.UserController.logtest(UserController.java:68) [classes/:?] at com.ttbank.flep.controller.UserController$$FastClassBySpringCGLIB$$adad7f81.invoke(<generated>) [classes/:?]
注意:一般再加一行, e.printStackTrace() 会把 stack trace 信息直接输出到控制台。
参考文献:
https://blog.csdn.net/jiangyyyeff/article/details/122580977
https://blog.csdn.net/qq_19642249/article/details/88538303
标签:log,Springcloud,信息,---,logtest,error,UserController,异常 From: https://www.cnblogs.com/luckyplj/p/18070700