log4j rootLogger 似乎继承了其他记录器的日志级别。为什么?

发布于 2024-08-25 06:25:50 字数 995 浏览 2 评论 0原文

我有一个 log4J 设置,其中根记录器应该将错误级别及以上级别的消息记录到控制台,另一个记录器将所有内容记录到系统日志。

log4j.properties 是:

# 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 的级别以某种方式覆盖了根记录器设置的级别。我想不通。

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

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 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

In code I do

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

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.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一个人的旅程 2024-09-01 06:25:57

在每个appender的基础上设置阈值是这里的关键:

log4j.appender.SGSearch.Threshold=DEBUG

log4j.appender.R.Threshold=ERROR

这是一个远比禁用“可加性”的建议更好的解决方案 - 问题是您想要多个附加程序处理的任何内容都会被它击败 - 所以您不能,例如,发送任何 如果将 additivity 设置为 false,则控制台和系统日志都会收到 code>ERROR 级别消息...

setting the threshold on a per-appender basis is key here:

log4j.appender.SGSearch.Threshold=DEBUG

and

log4j.appender.R.Threshold=ERROR

this is a far better solution than the suggestion to disable "additivity" - the problem with that is that anything you want multiple appenders to handle will be defeated by it - so you cannot, for example, send anything which is an ERROR level message to both the console and syslog if you set additivity to false...

烟柳画桥 2024-09-01 06:25:55

有关级别的快速信息

Log4J 级别

记录器可以被分配级别。这
一组可能的级别,即 DEBUG,
INFO、WARN、ERROR 和 FATAL 是
在 org.apache.log4j.Level 中定义
类。

如果给定的记录器没有分配
级别,然后它从其继承一个
有指定的最近祖先
级别。

根记录器位于
记录器层次结构。它始终存在
并且总是有指定的级别。

我已经更改了您的示例 log4j 配置以这种方式工作:

# Root logger option
log4j.rootLogger=ALL,R

log4j.appender.R=org.apache.log4j.ConsoleAppender
log4j.appender.R.Target=System.out
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%d %p %t %c - %m%n
log4j.appender.R.Threshold=ERROR

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
log4j.appender.SGSearch.Threshold=DEBUG

我希望这对您有帮助。

Quick information about Levels

Log4J Levels

Loggers may be assigned levels. The
set of possible levels, that is DEBUG,
INFO, WARN, ERROR and FATAL are
defined in the org.apache.log4j.Level
class.

If a given logger is not assigned a
level, then it inherits one from its
closest ancestor with an assigned
level.

The root logger resides at the top of
the logger hierarchy. It always exists
and always has an assigned level.

I've changed your sample log4j configuration to work this way:

# Root logger option
log4j.rootLogger=ALL,R

log4j.appender.R=org.apache.log4j.ConsoleAppender
log4j.appender.R.Target=System.out
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%d %p %t %c - %m%n
log4j.appender.R.Threshold=ERROR

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
log4j.appender.SGSearch.Threshold=DEBUG

I hope this helps you.

眼角的笑意。 2024-09-01 06:25:54

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

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

log4j.additivity.SGSearch=false

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

另一个建议:不要将您的记录器和附加程序命名为相同的名称,因为在将来的某个时候您或同事会将它们混淆。记录器名称应指示处理哪种类型的日志记录,附加程序名称应指定日志记录的位置。因此,在这种情况下,我认为“SGSearch”可能是记录器名称,而附加程序应该称为“LocalSysLog”之类的名称。

顺便说一句:在我看来,您通过使用高阈值限制根记录器并降低特定记录器的阈值来做正确的事情。这可以避免吵闹的库造成的混乱(Apache 有一些臭名昭著的库)。

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

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

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: 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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文