java异常日志记录

java的日志打印使用默认的e.printStackTrace()时,有时只能打印的空信息,项目上线后,针对日志做分析时如果有这样的情况挺尴尬,对于此情况可以通过如下方式来处理

e.printStackTrace();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
e.printStackTrace(new PrintStream(baos));
String exception = baos.toString();
//exception即异常的全部内容,在此处可以将其保存在日志文件中
logger.info(exception);
System.out.println("exception:" + exception);

猜你喜欢

转载自thoreau.iteye.com/blog/2255742