如何用一个log4j配置文件创建两个单独的日志文件?

我不知道如何配置我的log4j,以便我的debugLog和reportLog彼此分开(不是附加的)。为什么在以下配置中,reportsLog始终为空?

log4j.rootLogger=TRACE, stdout, debugLog

log4j.appender.stdout=org.apache.log4j.ConsoleAppender

log4j.appender.stdout.layout=org.apache.log4j.PatternLayout

log4j.appender.stdout.layout.ConversionPattern=%d [%24F:%t:%L] - %m%n

log4j.appender.debugLog=org.apache.log4j.FileAppender

log4j.appender.debugLog.File=logs/debug.log

log4j.appender.debugLog.layout=org.apache.log4j.PatternLayout

log4j.appender.debugLog.layout.ConversionPattern=%d [%24F:%t:%L] - %m%n

log4j.reportsLog=DEBUG,reportsLog

log4j.appender.reportsLog=org.apache.log4j.FileAppender

log4j.appender.reportsLog.File=logs/reports.log

log4j.appender.reportsLog.layout=org.apache.log4j.PatternLayout

log4j.appender.reportsLog.layout.ConversionPattern=%d [%24F:%t:%L] - %m%n

这是我的Java代码:

package test;

import org.apache.log4j.Logger;

import org.apache.log4j.PropertyConfigurator;

public class HelloLogger {

static final Logger debugLog = Logger.getLogger("debugLog");

static final Logger resultLog = Logger.getLogger("reportsLog");

public static void main(String[] args) {

PropertyConfigurator.configure("log4j.properties");

debugLog.debug("Hello debugLog message");

resultLog.debug("Hello reportsLog message");

}

}

回答:

尝试以下配置:

log4j.rootLogger=TRACE, stdout

log4j.appender.stdout=org.apache.log4j.ConsoleAppender

log4j.appender.stdout.layout=org.apache.log4j.PatternLayout

log4j.appender.stdout.layout.ConversionPattern=%d [%24F:%t:%L] - %m%n

log4j.appender.debugLog=org.apache.log4j.FileAppender

log4j.appender.debugLog.File=logs/debug.log

log4j.appender.debugLog.layout=org.apache.log4j.PatternLayout

log4j.appender.debugLog.layout.ConversionPattern=%d [%24F:%t:%L] - %m%n

log4j.appender.reportsLog=org.apache.log4j.FileAppender

log4j.appender.reportsLog.File=logs/reports.log

log4j.appender.reportsLog.layout=org.apache.log4j.PatternLayout

log4j.appender.reportsLog.layout.ConversionPattern=%d [%24F:%t:%L] - %m%n

log4j.category.debugLogger=TRACE, debugLog

log4j.additivity.debugLogger=false

log4j.category.reportsLogger=DEBUG, reportsLog

log4j.additivity.reportsLogger=false

然后相应地用Java代码配置记录器:

static final Logger debugLog = Logger.getLogger("debugLogger");

static final Logger resultLog = Logger.getLogger("reportsLogger");

你要输出到stdout吗?如果不是,请将第一行更改log4j.properties为:

log4j.rootLogger=OFF

并摆脱stdout界限。

以上是 如何用一个log4j配置文件创建两个单独的日志文件? 的全部内容, 来源链接: utcz.com/qa/413237.html

回到顶部