本文介绍了如何使用一个log4j配置文件创建2个单独的日志文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我无法弄清楚如何配置我的log4j,以便我的debugLog和我的reportLog彼此分开(不是附加的)。为什么,在下面的配置中,reportsLog始终为空?
I can't figure out how to configure my log4j so that my debugLog and my reportsLog are separate from each other (not additive). Why is it, that in the configuration below, the reportsLog is always empty?
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代码:
And here is my Java code:
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代码中的记录器:
Then configure the loggers in the Java code accordingly:
static final Logger debugLog = Logger.getLogger("debugLogger");
static final Logger resultLog = Logger.getLogger("reportsLogger");
您希望输出转到标准输出
?如果没有,请将 log4j.properties
的第一行更改为:
Do you want output to go to stdout
? If not, change the first line of log4j.properties
to:
log4j.rootLogger=OFF
并摆脱 stdout
行。
这篇关于如何使用一个log4j配置文件创建2个单独的日志文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!