Log4j不同等級輸入不同檔案
主要是利用Threshold.
Threshold的語法
log4j.threshold=level
level就是前面所說的 OFF ,FATAL ,ERROR ,WARN ,INFO ,DEBUG ,ALL。Threshold是個全域性的過濾器,它將把低於所設定的level的資訊過濾不顯示出來。
看一個例子:
log4j.rootLogger=DEBUG, CON
log4j.appender.CON=org.apache.log4j.ConsoleAppender
log4j.appender.CON.layout=org.apache.log4j.PatternLayout
log4j.appender.CON.layout.ConversionPattern=[%t] %-5p %c - %m%n
#Only print log statement of level WARN or above regardless of the(只打印warn級別以上的資訊)
#logger.
log4j.threshold=WARN
Threshold的用法很靈活,還可以在appender中增加threshold
log4j.rootLogger=DEBUG, C
log4j.appender.C=org.apache.log4j.ConsoleAppender
# Set the appender threshold to INFO
log4j.appender.C.Threshold=INFO
log4j.appender.C.layout=org.apache.log4j.PatternLayout
log4j.appender.C.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
這裡初始設定 level為DEBUG ,但log4j.appender.C.Threshold=INFO 設定為INFO。因為 INFO>DEBUG 所以如果含有DEBUG級別的資訊,將被過濾。
log4j.rootLogger=debug, stdout, R log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=[%d] [%4p] %l %m%n log4j.appender.R=org.apache.log4j.RollingFileAppender log4j.appender.R.Encoding=GBK log4j.appender.R.Threshold=info log4j.appender.R.File=cohl_elearning.log #..//oil//applications//cohlElearning//cohlElearning// log4j.appender.R.MaxFileSize= 2048KB log4j.appender.R.MaxBackupIndex=20 log4j.appender.R.layout=org.apache.log4j.PatternLayout log4j.appender.R.layout.ConversionPattern=[%d] [%t] [%4p] %l %m%n
public class TestJava {
static Logger logger = Logger.getLogger(TestJava.class);
/**
* @param args
*/
public static void main(String[] args) {
loggerTest();
}
public static void loggerTest() {
logger.info("logger info");
logger.debug("logger debug");
logger.error("logger error");
}
}