2

我正在尝试将我的信息级别日志和错误级别日志存储在单独的文件中。我的错误消息被正确转储,但信息消息包含错误和信息消息。以下是我的属性文件:

log4j.rootLogger = DEBUG, inf, err 

log4j.appender.inf = org.apache.log4j.FileAppender
log4j.appender.inf.layout = org.apache.log4j.PatternLayout
log4j.appender.inf.layout.ConversionPattern = %d{ISO8601} %-5p %c ~ %m%n
log4j.appender.inf.File = information.log
log4j.appender.inf.Threshold = INFO
log4j.appender.inf.filter = org.apache.log4j.varia.LevelMatchFilter
log4j.appender.inf.filter.levelToMatch = INFO
log4j.appender.inf.Append = false
log4j.appender.inf.AcceptOnMatch = true
log4j.appender.inf.filter = org.apache.log4j.varia.DenyAllFilter

log4j.appender.err = org.apache.log4j.FileAppender
log4j.appender.err.layout = org.apache.log4j.PatternLayout
log4j.appender.err.layout.ConversionPattern = %d{ISO8601} %-5p %c ~ %m%n
log4j.appender.err.File = errormsg.log
log4j.appender.err.Threshold = ERROR
log4j.appender.err.filter = org.apache.log4j.varia.LevelMatchFilter
log4j.appender.err.filter.levelToMatch = ERROR
log4j.appender.err.Append = false

我的 .java 文件如下:

package test;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

public class HelloLogger {

     private static Logger logger = Logger.getLogger(HelloLogger.class);

        public static void main(String[] args) {
            PropertyConfigurator.configure("log4jconfig.properties");
            logger.error("This is error");
            logger.info("This is info");
            logger.debug("This is debug");
        } 
}

请帮助.. 提前谢谢

4

1 回答 1

0

根据这个答案,属性文件不是为处理过滤器而设计的,但是如果您更改属性文件以将第一个附加程序的过滤器与 ID(即 1 和 2)分开,它可能会起作用:

log4j.appender.inf.filter.1 = org.apache.log4j.varia.LevelMatchFilter
log4j.appender.inf.filter.1.levelToMatch = INFO
log4j.appender.inf.Append = false
log4j.appender.inf.AcceptOnMatch = true
log4j.appender.inf.filter.2 = org.apache.log4j.varia.DenyAllFilter
于 2013-07-16T17:36:28.310 回答