标签:
当程序运行异常时,e.printStackTrace()会打印出异常。但是很多时候我们希望将这些异常输出到日志中,以便日后可以随时查看到,可以通过这些异常快速的找到程序发生异常的代码。public static String getTrace(Throwable t) { StringWriter stringWriter= new StringWriter(); PrintWriter writer= new PrintWriter(stringWriter); t.printStackTrace(writer); StringBuffer buffer= stringWriter.getBuffer(); return buffer.toString(); }
try(){ }catch(IoException e) e.printStackTrace(); logger.error(getTrace(e)); //将异常输出到文件 }
标签:
原文地址:http://blog.csdn.net/wm5920/article/details/45363207