使用log4j打印异常信息:
log4j.properties配置:
log4j.rootLogger=INFO, stdout, logfile
#,error,stdout, logfile
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n
log4j.appender.logfile=org.apache.log4j.DailyRollingFileAppender
log4j.appender.logfile.File=Log/debug.log
log4j.appender.logfile.DatePattern = '.'yyyy-MM-dd
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n
java代码:
public class XwpfServicesImpl{
private static Logger logger;
public static void main(String[] arg){
logger = Logger.getLogger(XwpfServicesImpl.class);
//使用自定义的log4j配置
PropertyConfigurator.configure("source/log4j.properties");
try{
........
}catch(Exception ex){
logger.error("error",ex);
ex.getMessage();
}
}
}
打印异常样式如下:
2016-06-13 13:45:27,647 ERROR [com.web.server.impl.XwpfServicesImpl] - error
java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.ArrayList.rangeCheck(Unknown Source)
at java.util.ArrayList.get(Unknown Source)
at com.web.server.impl.XwpfServicesImpl.main(XwpfServicesImpl.java:347)
代码粗糙,仅仅用于自己记录而已,请见谅!