java 日志中打印异常信息

java程序中 执行遇到的异常,通过e.printStackTrace(); 会打印在控制台。那怎么将异常信息e.printStackTrace();打印到日志中呢。

百度到了一位大神博主解决了这一问题:https://blog.csdn.net/hongweigg/article/details/18313461

ByteArrayOutputStream baos = new ByteArrayOutputStream();
e.printStackTrace(new PrintStream(baos));
String exception = baos.toString();
LOGGER.error("异常信息为:" + exception);

通过文件流来保存异常信息。

你可能感兴趣的:(Java)