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");
    	
    }
}


你可能感兴趣的:(c,log4j,String,Class)