用log4j在日志文件里打印异常

Java工具类:

public class ExceptionConstans { 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(); } }

 

程序里调用:

log.error("/n异常信息如下:/n" + ExceptionConstans.getTrace(e));

 

或:

log.error("/n异常信息如下:/n" + ExceptionUtils.getFullStackTrace(e));

你可能感兴趣的:(java,log4j,String,Class,buffer,工具)