使用log4j记录单独的日志文件

时间:2013-07-16 05:31:15

标签: log4j

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

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

请提前帮助.. Thanx

1 个答案:

答案 0 :(得分:0)

根据this answer,属性文件不是为处理过滤器而设计的,但如果您更改属性文件以将第一个appender的过滤器与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