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发生了改变
<?xml version="1.0" encoding="UTF-8"?> <classpath> <classpathentry kind="src" path="src"/> <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/> <classpathentry kind="lib" path="lib/commons-logging-1.0.4.jar"/> <classpathentry kind="lib" path="lib/log4j.jar"/> <classpathentry kind="output" path="bin"/> </classpath>
2 添加 \bin\log4j.properties
# log4j configuration used during build and unit tests log4j.rootLogger=info,stdout,R log4j.threshhold=ALL log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d{ISO8601} %-5p %c{2} (%F:%M(%L)) - %m%n log4j.appender.R=org.apache.log4j.RollingFileAppender log4j.appender.R.File=test.txt log4j.appender.R.MaxFileSize=100KB log4j.appender.R.MaxBackupIndex=0 log4j.appender.R.layout=org.apache.log4j.PatternLayout log4j.appender.R.layout.ConversionPattern=%d{ISO8601} %-5p %c{2} (%F:%M(%L)) - %m%n
运行,日志文件位置:log4jtest\test.txt