学习链接:
http://www.codeceo.com/article/log4j-usage.html
http://www.blogjava.net/kit-soft/archive/2009/08/28/292977.html
http://www.cnblogs.com/ITEagle/archive/2010/04/23/1718365.html
log4j文件:
log4j.rootLogger=info,C,R log4j.appender.C=org.apache.log4j.ConsoleAppender log4j.appender.C.Target=System.out log4j.appender.C.layout=org.apache.log4j.PatternLayout log4j.appender.C.layout.ConversionPattern=%-d %5p [%C.java:%L] - %m%n log4j.appender.R=org.apache.log4j.RollingFileAppender log4j.appender.R.File=D://logs/nisweb.log log4j.appender.R.MaxFileSize=5MB log4j.appender.R.MaxBackupIndex=2 log4j.appender.R.layout=org.apache.log4j.PatternLayout log4j.appender.R.layout.ConversionPattern=%-d %5p [%C.java:%L] - %m%n
测试:
package com.mucfc; import org.apache.log4j.Logger; public class Test { private static Logger logger = Logger.getLogger(Test.class); public void logFunction(){ logger.info("111122"); } public static void main(String[] args) { // PropertyConfigurator.configure(ClassLoader.getSystemResource("log4j.properties")); System.out.println(ClassLoader.getSystemResource("log4j.properties")); // Logger logger = Logger.getLogger(Test.class); logger.info("message info"); logger.error("message error"); } }
结果:
log4j.properties地址: file:/D:/work/workspace/LogLearning/build/classes/log4j.properties 2016-03-08 15:01:32,769 INFO [com.mucfc.Test.java:17] - message info 2016-03-08 15:01:32,771 ERROR [com.mucfc.Test.java:18] - message error