大家知道 e.printStackTrace() 可以打印异常堆栈信息 ,那么 logger.error() 怎么输出呢
正确写法
try { } catch (ArgsException e) { logger.error("异常",e); }
错误写法
logger.error(e.getMessage());
logger.error(e.toString());
logger.error("异常" + e);
看源码
/** * Log a message at the ERROR level. * * @param msg the message string to be logged */ public void error(String msg);
/** * Log an exception (throwable) at the ERROR level with an * accompanying message. * * @param msg the message accompanying the exception * @param t the exception (throwable) to log */ public void error(String msg, Throwable t);
可以看出如果传入字符串是不会打印异常信息的
本文讲解了如何在Java中正确地使用logger.error()方法来记录异常信息,对比了正确的做法和常见错误,强调了直接传入异常对象的重要性。
1029

被折叠的 条评论
为什么被折叠?



