本例的配置文件如下,:
log4j.rootLogger = info,stdout,all
log4j.appender.stdout = org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target = System.out
log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern = [%5p] %d{yyyy-MM-dd HH:mm:ss,SSS} method:%l%n%m%n
log4j.appender.all = org.apache.log4j.DailyRollingFileAppender
log4j.appender.all.File = ../logs/all.log
log4j.appender.all.Append = true
log4j.appender.all.Threshold = error
log4j.appender.all.layout = org.apache.log4j.PatternLayout
log4j.appender.all.layout.ConversionPattern = %d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] [%c] [%l] %m%n
log4j.logger.subDebugLogger=debug, file1appender
log4j.appender.file1appender = org.apache.log4j.DailyRollingFileAppender
log4j.appender.file1appender.File =../logs/debug.log
log4j.appender.file1appender.Append = true
log4j.appender.file1appender.Threshold = debug
log4j.appender.file1appender.layout = org.apache.log4j.PatternLayout
log4j.appender.file1appender.layout.ConversionPattern = %d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] [%c] [%l] %m%n
log4j.additivity.subDebugLogger=false
log4j.logger.subInfoLogger=info, file2appender
log4j.appender.file2appender = org.apache.log4j.DailyRollingFileAppender
log4j.appender.file2appender.File =../logs/info.log
log4j.appender.file2appender.Append = true
log4j.appender.file2appender.Threshold = info
log4j.appender.file2appender.layout = org.apache.log4j.PatternLayout
log4j.appender.file2appender.layout.ConversionPattern = %d{yyyy-MM-dd HH:mm:ss,SSS} [ %t:%r ] - [ %p ] [%c] [%l] %m%n
log4j.additivity.subInfoLogger=false
log4j.logger.subErrorLogger=error, file3appender
log4j.appender.file3appender = org.apache.log4j.DailyRollingFileAppender
log4j.appender.file3appender.File =../logs/error.log
log4j.appender.file3appender.Append = true
log4j.appender.file3appender.Threshold = error
log4j.appender.file3appender.layout = org.apache.log4j.PatternLayout
log4j.appender.file3appender.layout.ConversionPattern = %d{yyyy-MM-dd HH:mm:ss,SSS} [ %t:%r ] - [ %p ] [%c] [%l] %m%n
log4j.additivity.subErrorLogger=false
package log4jtest;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
public class test2 {
public static void main(String[] args) {
// Logger logger = Logger.getLogger ( test2.class.getName() );
// 读取配置文件,配置Log4j环境
PropertyConfigurator.configure(Thread.currentThread().getContextClassLoader().getResource("./conf/log4j.properties"));
Logger.getRootLogger().debug("DDD");
Logger.getRootLogger().info("EEE");
Logger.getRootLogger().error("FFF");
Logger.getLogger("subDebugLogger").debug("GGG");
Logger.getLogger("subDebugLogger").info("HHH");
Logger.getLogger("subDebugLogger").error("III");
Logger.getLogger("subInfoLogger").debug("JJJ");
Logger.getLogger("subInfoLogger").info("KKK");
Logger.getLogger("subInfoLogger").error("LLL");
Logger.getLogger("subErrorLogger").debug("MMM");
Logger.getLogger("subErrorLogger").info("NNN");
Logger.getLogger("subErrorLogger").error("OOO");
}
}
五、运行调试,运行结果如下,以下是标准输出,当然还有几个文件输出,这里不截图了。
[ INFO] 2017-07-14 16:15:55,417 method:log4jtest.test2.main(test2.java:15)
EEE
[ERROR] 2017-07-14 16:15:55,425 method:log4jtest.test2.main(test2.java:16)
FFF
http://www.cnblogs.com/ITtangtang/p/3926665.html
http://www.codeceo.com/article/log4j-usage.html