Log4j:多个记录器,级别和附加器 [英] Log4j : Multiple loggers, levels and appenders
问题描述
使用log4j写入多个日志文件时,我遇到重复日志消息的麻烦.
I am having trouble with duplicate log messages when writing to multiple log files using log4j.
目前,我正尝试在我的foo.log
文件中记录名为 foobar 的特定记录器的INFO
级别数据(及更高版本),然后记录所有WARN
级别的日志消息(及更高版本) )用于bar.log
文件中的所有记录器.
At present I am trying to log INFO
level data (and upwards) for the specific logger named foobar in my foo.log
file and then all WARN
level log messages (and upwards) for all loggers in the bar.log
file.
因此,重复的日志消息被写入foo.log
文件(每行记录两次),经过快速研究,我发现解决此问题的建议是将log4j.additivity.foobar=false
添加到我的属性文件中
As a result of this, duplicate log messages were written to the foo.log
file (each line was logged twice) and after some quick research I found that the suggestion to fix this was to add log4j.additivity.foobar=false
to my properties file.
此问题是,尽管它停止了重复的行,但 foobar 记录器中的WARN
消息却永远不会写入bar.log
文件.
The problem with this is that although it stops duplicate lines, the WARN
message from the foobar logger are never written to the bar.log
file.
我的log4j属性文件如下:
log4j.rootLogger = WARN, FOO, BAR
log4j.logger.foobar = INFO, FOO
log4j.additivity.foobar = false
log4j.appender.FOO = org.apache.log4j.RollingFileAppender
log4j.appender.FOO.layout = org.apache.log4j.PatternLayout
log4j.appender.FOO.layout.ConversionPattern = %d{ISO8601} %-5p %c ~ %m%n
log4j.appender.FOO.File = foo.log
log4j.appender.BAR = org.apache.log4j.RollingFileAppender
log4j.appender.BAR.layout = org.apache.log4j.PatternLayout
log4j.appender.BAR.layout.ConversionPattern = %d{ISO8601} %-5p %c ~ %m%n
log4j.appender.BAR.File = bar.log
有人知道我如何将日志消息写入两个日志文件(就像在开始设置additivity
属性之前所做的那样)并且仍然防止重复的日志消息吗?
Does anyone know how I can write the log messages to both log files (as it was doing before I started setting the additivity
property) and still prevent the duplicate log messages?
请注意,这是问题的简化摘要.在现实世界中,有多个记录器和两个以上的日志文件
推荐答案
此问题可以分为两部分来解决.
This problem can be solved in two parts.
1.防止重复的日志消息
日志消息被写入两次,因为我们在 rootLogger 和 log4j.logger.foobar 类别中都列出了 FOO 附加程序.因此,我们必须删除附加程序,仅在类别中定义日志记录级别:
The log messages were written twice because we listed the FOO appender in both the rootLogger and the log4j.logger.foobar category. So we must remove the appender and only define the logging level in category:
log4j.rootLogger = WARN, FOO, BAR
log4j.logger.foobar = INFO
这意味着来自 log4j.logger.foobar 的 INFO 级消息将向上传播到 appenders 中的所有记录器> rootLogger ,但只会写入到每个日志文件一次.
This means that the INFO level messages from log4j.logger.foobar are propagated upwards to ALL of the loggers the appenders in rootLogger, but will only be written to each log file once.
2.防止将INFO级消息写入bar.log
由于 log4j.logger.foobar 类别的所有INFO级别日志消息均已由 rootLogger 中的附加程序继承,因此我们需要停止BAR
记录 INFO 级消息的附加程序.
Since all of the INFO level log messages for the log4j.logger.foobar category are being inherited by the appenders in rootLogger, we need to stop the BAR
appender for recording the INFO level messages.
我们可以通过在 BAR 附加器本身上设置 Threshold 属性来实现此目的:
We can achieve this by setting the Threshold property on the BAR appender itself:
log4j.appender.BAR.Threshold = WARN
这将防止 INFO 级别的语句记录在 bar.log 文件中,因为它仅接受 WARN 和更高级别.
This will prevent the INFO level statements being logged in the bar.log file as it will only accept levels of WARN and upwards.
因此,完整的log4j属性文件如下:
log4j.rootLogger = WARN, FOO, BAR
log4j.logger.foobar = INFO
log4j.appender.FOO = org.apache.log4j.RollingFileAppender
log4j.appender.FOO.layout = org.apache.log4j.PatternLayout
log4j.appender.FOO.layout.ConversionPattern = %d{ISO8601} %-5p %c ~ %m%n
log4j.appender.FOO.File = foo.log
log4j.appender.FOO.Threshold = INFO
log4j.appender.BAR = org.apache.log4j.RollingFileAppender
log4j.appender.BAR.layout = org.apache.log4j.PatternLayout
log4j.appender.BAR.layout.ConversionPattern = %d{ISO8601} %-5p %c ~ %m%n
log4j.appender.BAR.File = bar.log
log4j.appender.BAR.Threshold = WARN
这篇关于Log4j:多个记录器,级别和附加器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!