package test;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;public class Log4jTest { private static Log logger = LogFactory.getLog(Log4jTest.class); public static void main(String[] args) { logger.error("ERROR"); logger.debug("DEBUG"); logger.warn("WARN"); logger.info("INFO"); logger.trace("TRACE"); System.out.println(logger.getClass()); }}
1 将commons-logging-1.0.4.jar,log4j.jar放入新建文件夹lib下,然后选中二者,build path 后,classpath发生了改变
2 添加 \bin\log4j.properties
# log4j configuration used during build and unit testslog4j.rootLogger=info,stdout,Rlog4j.threshhold=ALLlog4j.appender.stdout=org.apache.log4j.ConsoleAppenderlog4j.appender.stdout.layout=org.apache.log4j.PatternLayoutlog4j.appender.stdout.layout.ConversionPattern=%d{ISO8601} %-5p %c{2} (%F:%M(%L)) - %m%nlog4j.appender.R=org.apache.log4j.RollingFileAppenderlog4j.appender.R.File=test.txtlog4j.appender.R.MaxFileSize=100KBlog4j.appender.R.MaxBackupIndex=0log4j.appender.R.layout=org.apache.log4j.PatternLayoutlog4j.appender.R.layout.ConversionPattern=%d{ISO8601} %-5p %c{2} (%F:%M(%L)) - %m%n
运行,日志文件位置:log4jtest\test.txt