使用Console-和多个FileAppender的log4j可加性

时间:2013-04-26 08:44:53

标签: log4j

今天我发现了如何配置我的log4j.properties来登录不同的文件:

log4j.rootLogger=ALL, ConsoleAppender, FA1, FA2, FA3

#Define which packages use which appenders
log4j.logger.de.package1=ALL, FA1
log4j.logger.de.package2=ALL, FA2
log4j.logger.de.package3=ALL, FA3

#Ensure the logs don't add to each other
log4j.additivity.de.package1=false
log4j.additivity.de.package2=false
log4j.additivity.de.package3=false

#If you want to append the file(not to override when it already exists):
#log4j.appender.FileAppender.Append=true

log4j.appender.ConsoleAppender=org.apache.log4j.ConsoleAppender
log4j.appender.ConsoleAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.ConsoleAppender.layout.ConversionPattern=%d (%F:%L) %-5p %c - %m%n

log4j.appender.FA1=org.apache.log4j.FileAppender
log4j.appender.FA1.Threshold=WARN
log4j.appender.FA1.File=./logs/FA1.log
log4j.appender.FA1.layout=org.apache.log4j.PatternLayout
log4j.appender.FA1.layout.ConversionPattern=%d (%F:%L) %-5p %c - %m%n

log4j.appender.FA2=org.apache.log4j.FileAppender
log4j.appender.FA2.Threshold=WARN
log4j.appender.FA2.File=./logs/FA2.log
log4j.appender.FA2.layout=org.apache.log4j.PatternLayout
log4j.appender.FA2.layout.ConversionPattern=%d (%F:%L) %-5p %c - %m%n

log4j.appender.FA3=org.apache.log4j.FileAppender
log4j.appender.FA3.Threshold=WARN
log4j.appender.FA3.File=./logs/FA3.log
log4j.appender.FA3.layout=org.apache.log4j.PatternLayout
log4j.appender.FA3.layout.ConversionPattern=%d (%F:%L) %-5p %c - %m%n

正如您所看到的,我将可加性定义为false但是我有问题 没有控制台输出了。

如果我将additivity设置为true,则所有消息都会被记录两次 每个fileappender。

我该如何解决这个问题?我不想要双重输入,我不想要每条消息 在每个日志文件中,但我希望在我的控制台中拥有所有消息。

有点棘手还是比我想的更简单?

1 个答案:

答案 0 :(得分:1)

找到解决方案:

#Define which packages use which appenders
log4j.logger.de.package1=ALL, FA1, ConsoleAppender
log4j.logger.de.package2=ALL, FA2, ConsoleAppender
log4j.logger.de.package3=ALL, FA3, ConsoleAppender

我还需要为每个包添加ConsoleAppender。