使用 @Log4j2 log.error() 打印异常日志
public static void main(String[] args) { int a = 10; try { int i = 1/0; } catch (Exception e) { System.out.println("--------------------"); e.printStackTrace();// 只能输出在控制台当中,日志文件看不到 System.out.println("--------------------"); log.error(e.getMessage());// 只能输出简短的错误信息,不便于排错 System.out.println("--------------------"); log.error(e.getStackTrace().toString());// 不能输出错误信息 System.out.println("--------------------"); log.error("需要在错误日志 error 之上打锚点,报错了,a-{}",a); //用这个去打印日志,完整的日志信息会被打印在日志之中,相当于 e.printStackTrace(); log.error("test fail-",e);// 可以在日志中输出完整的错误信息,""里要写内容,不能在此处去打错误锚点(不能使用log.error("test fail-{},{}",a,e))无法打出全部日志 System.out.println("--------------------"); log.error("test fail-{},{}",a,e);//无法打出全部日志 System.out.println("--------------------"); log.error(e.toString());// 只能输出简短的错误信息,不便于排错 } }
-------------------- java.lang.ArithmeticException: / by zero at com.jn.ssr.superrescue.web.order.service.impl.OrderBasicServiceImpl.main(OrderBasicServiceImpl.java:2056) main 2019.11.01 at 10:50:51.331 CST [] ERROR com.jn.ssr.superrescue.web.order.service.impl.OrderBasicServiceImpl main():2062 - / by zero -------------------- main 2019.11.01 at 10:50:51.350 CST [] ERROR com.jn.ssr.superrescue.web.order.service.impl.OrderBasicServiceImpl main():2064 - [Ljava.lang.StackTraceElement;@5fd4f8f5 -------------------- main 2019.11.01 at 10:50:51.356 CST [] ERROR com.jn.ssr.superrescue.web.order.service.impl.OrderBasicServiceImpl main():2066 - 需要在错误日志 error 之上打锚点,报错了,a-10 main 2019.11.01 at 10:50:51.357 CST [] ERROR com.jn.ssr.superrescue.web.order.service.impl.OrderBasicServiceImpl main():2067 - test fail- java.lang.ArithmeticException: / by zero at com.jn.ssr.superrescue.web.order.service.impl.OrderBasicServiceImpl.main(OrderBasicServiceImpl.java:2056) [classes/:?] -------------------- main 2019.11.01 at 10:50:51.365 CST [] ERROR com.jn.ssr.superrescue.web.order.service.impl.OrderBasicServiceImpl main():2069 - test fail-10,java.lang.ArithmeticException: / by zero -------------------- main 2019.11.01 at 10:50:51.366 CST [] ERROR com.jn.ssr.superrescue.web.order.service.impl.OrderBasicServiceImpl main():2071 - java.lang.ArithmeticException: / by zero