今天我发现了如何配置我的 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 ,但是我遇到了不再有控制台输出的问题。
如果我将可加性设置为 true,则所有消息都会记录两次并在每个 fileappender 中。
我该如何解决这个问题?我不想要双重条目,也不想要每个日志文件中的每条消息,但我希望在我的控制台中包含所有消息。
有点棘手还是比我想象的更简单?