前情提要:今天要定位项目中一个问题,发现该打日志的地方没打出来;所以除了bug本身的问题,还要把项目日志操作理顺。用的是org.apache.log4j.Logger;这里做一个简短的lazyguid。
用法:
package com.demo.log4j;
import org.apache.log4j.Logger;
public class TestLog4j {
private static Logger logger = Logger.getLogger(TestLog4j.class);
public static void testLog4j() {
logger.debug("This is debug message.");
logger.info("This is info message.");
logger.error("This is error message.");
}
}
配置文件:log4j.properties
# 配置日志的等级,目的地
log4j.rootLogger=info,CONSOLE,LOGFILE
# CONSOLE is set to be a ConsoleAppender.
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=[%d{yyyy-MM-dd HH:mm:ss}] [%C{1}.%M -%p %L] %m%n
#log4j.appender.CONSOLE.Threshold=ERROR
# LOGFILE is set to be a file
log4j.appender.LOGFILE=org.apache.log4j.DailyRollingFileAppender
log4j.appender.LOGFILE.layout=org.apache.log4j.PatternLayout
log4j.appender.LOGFILE.layout.ConversionPattern=[%d{yyyy-MM-dd HH:mm:ss}] [%c{1}-%p %L] %m%n
# 这里配置日志文件的路径,也可以用变量 ${java.home}、 rolling.log
log4j.appender.LOGFILE.File=d:/Log4j/log4j.log
#log4j.appender.LOGFILE.Threshold=ERROR
log4j.appender.LOGFILE.Encoding=UTF-8
log4j.appender.LOGFILE.DatePattern='.'yyyy-MM-dd
# 这里配置日志增加方式;true 添加 false 覆盖
log4j.appender.LOGFILE.Append=true
log4j.logger.org.docx4j=off
log4j.logger.org.docx4j.jaxb=off
log4j.logger.org.docx4j.document.wordprocessingml=off
log4j.logger.org.docx4j.openpackaging=off
log4j.logger.org.docx4j.openpackaging.packages=off
log4j.logger.org.docx4j.openpackaging.io=off
log4j.logger.org.docx4j.openpackaging.parts=off
log4j.logger.org.docx4j.fonts=off
log4j.logger.org.docx4j.convert.out.html.HtmlExporterNG2=off
log4j.logger.org.docx4j.convert.out.pdf.viaXSLFO=off
网上这篇讲的比较详细:http://www.iteye.com/topic/378077