log4j rootLogger似乎继承了其他logger的日志级别。为什么? [英] log4j rootLogger seems to inherit log level of other logger. Why?

查看:85
本文介绍了log4j rootLogger似乎继承了其他logger的日志级别。为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个log4J设置,其中根记录器应该将ERROR级别消息和上面的消息记录到控制台,另一个记录器将所有内容记录到syslog。

I've got a log4J setup in which the root logger is supposed to log ERROR level messages and above to the console and another logger logs everything to syslog.

log4j.properties是:

log4j.properties is:

# Root logger option
log4j.rootLogger=ERROR,R

log4j.appender.R=org.apache.log4j.ConsoleAppender
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%d %p %t %c - %m%n

log4j.logger.SGSearch=DEBUG,SGSearch
log4j.appender.SGSearch=org.apache.log4j.net.SyslogAppender
log4j.appender.SGSearch.SyslogHost=localhost
log4j.appender.SGSearch.Facility=LOCAL6
log4j.appender.SGSearch.layout=org.apache.log4j.PatternLayout
log4j.appender.SGSearch.layout.ConversionPattern=[%-5p] %m%n

在代码中我做

private static final Logger logger = Logger.getLogger("SGSearch");
.
.
.
logger.info("Commencing snapshot index [" + args[1] + " -> " + args[2] + "]");

发生的事情是我获得了所有日志记录级别的控制台日志记录。似乎正在发生的事情是SGSearch的级别以某种方式覆盖了根记录器的级别设置。我无法理解。

What is happening is that I get the console logging for all logging levels. What seems to be happening is that the level for SGSearch overrides the level set for the root logger somehow. I can't figure it out.

我已经确认Log4J正在读取我认为的属性文件,而没有其他(通过 -Dlog4j.debug 选项)

I have confirmed that Log4J is reading the properties file I think it is, and no other (via the -Dlog4j.debug option)

推荐答案

Log4j链接的工作方式有点直观(对于我至少)。请参阅 log4j手册。如果请求级别等于或高于最具体的匹配记录器的阈值,则接受该级别。一旦请求被接受,它将由完整的祖先链处理,无论其阈值如何!

The way Log4j chaining works is a bit counter intuitive (to me at least). See the log4j manual. If the request level is equal to or above the threshold of the most specific matching logger, it is accepted. Once the request is accepted, it gets handled by the complete chain of ancestors regardless of their thresholds!

要抑制链接行为,请添加:

To suppress the chaining behavior, add:

log4j.additivity.SGSearch=false

这将导致记录器SGSearch处理的请求不再向上传递。

This will cause requests handled by logger SGSearch to no longer be passed up the chain.

另一个建议:不要将记录器和appender命名为相同,因为在未来的某个时刻,你或同事会混淆他们。记录器名称应指示处理哪种类型的日志记录,appender名称应指定日志记录的位置。所以在这种情况下,我认为'SGSearch'可能是记录器名称,而appender应该被称为'LocalSysLog'。

One other suggestion: don't name your logger and appender the same, because at some point in the future you, or a colleague will mix them up. The logger name should indicate which type of logging is handled, the appender name should specify where the logging goes. So in this case I would think 'SGSearch' could be the logger name, and the appender should be called something like 'LocalSysLog'.

BTW:在我看来你是通过限制具有高阈值的根记录器并针对特定记录器降低它来做正确的事情。这避免了大声库的混乱(Apache有一些臭名昭着的库)。

BTW: In my opinion you are doing the right thing by restricting the root logger with a high threshold, and lowering it for specific loggers. This avoids clutter from loud libraries (Apache has a few notorious ones).

这篇关于log4j rootLogger似乎继承了其他logger的日志级别。为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆