为什么 Log4j rootLogger 不根据事件级别过滤日志事件?

发布于 2024-09-05 06:30:36 字数 767 浏览 2 评论 0原文

为什么我的应用程序中的 Log4j rootLogger 不根据过滤日志事件到水平?在我的 log4j.properties 中,我有多个记录器:

log4j.rootLogger=info,stdout
log4j.logger.com.name.myapp=debug,myapp
log4j.logger.org.castor=debug,castor
log4j.logger.org.exolab.castor=debug,castor
log4j.logger.org.hibernate=debug,hibernate
log4j.logger.org.springframework=debug,spring

每个记录器都在 DEBUG 及以上级别接收并记录大量日志事件,这是我所期望和想要的。然而,尽管 rootLogger 设置为 INFO 级别,但它也显示所有这些事件,包括 DEBUG 事件,这不是我所期望的,而不是我所渴望的。相反,我希望它过滤 DEBUG 事件,但仅显示 INFO 及更高级别的事件(WARNERRORFATAL),这也是我想要的。为什么 rootLogger 显示所有事件?

Why is the Log4j rootLogger in my application not filtering log events according to level? In my log4j.properties, I have several loggers:

log4j.rootLogger=info,stdout
log4j.logger.com.name.myapp=debug,myapp
log4j.logger.org.castor=debug,castor
log4j.logger.org.exolab.castor=debug,castor
log4j.logger.org.hibernate=debug,hibernate
log4j.logger.org.springframework=debug,spring

Each of the loggers receive and record numerous log events at levels DEBUG and above, which is what I expect and desire. The rootLogger, however, despite being set to level INFO, is displaying all of these events, too, including the DEBUG events, which is not what I expect and not what I desire. Instead, I would expect it to filter the DEBUG events, but display only the events at level INFO and higher (WARN, ERROR, and FATAL), which is also what I want. Why is rootLogger displaying all of the events?

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

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

发布评论

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

评论(3

乖乖公主 2024-09-12 06:30:36

请参阅此答案 类似的问题 Log4j 中的记录器链接:

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

这意味着无论您将根记录器的阈值设置为什么级别,它都将始终接受并输出任何其他记录器接受的日志事件,除非您禁用该子记录器的链接或显式将其附加程序的阈值设置为更高的水平。

因此,在这种情况下,有两种方法可以阻止根记录器捕获来自其他记录器的事件。第一种是更有选择性的禁用日志事件链接的方法:

log4j.additivity.com.name.myapp=false
log4j.additivity.org.castor=false
log4j.additivity.org.exolab.castor=false
log4j.additivity.org.hibernate=false
log4j.additivity.org.springframework=false

第二种方法更简单,但限制性更强,因为它会抑制控制台上低于 INFO (DEBUG 和 TRACE):

log4j.appender.stdout.Threshold=info

See this answer to a similar question about logger chaining in Log4j:

The way Log4j chaining works is a bit
counter intuitive (to me at least). 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!

This means that no matter to what level you set the threshold of the root logger, it will always accept and output the log event that any other logger accepts, unless you disable chaining for that child logger or explicitly set the threshold of its appender to a higher level.

So, in this case, there are two ways that you can prevent root logger from capturing the events from the other loggers. The first is the more selective approach of disabling log event chaining:

log4j.additivity.com.name.myapp=false
log4j.additivity.org.castor=false
log4j.additivity.org.exolab.castor=false
log4j.additivity.org.hibernate=false
log4j.additivity.org.springframework=false

The second way is simpler, but more restrictive since it suppresses all events on the console that are lower than INFO (DEBUG and TRACE):

log4j.appender.stdout.Threshold=info
旧瑾黎汐 2024-09-12 06:30:36

查看简介中描述的继承。如果您在包级别指定级别,它将不会继承根记录器的级别。您在指定的包中使用调试,而不是信息。指定级别会覆盖继承的任何内容。

如果您想继承根记录器的级别,请删除记录器配置中的级别规范。

Check out the inheritance described in the intro. If you specify a level at the package level, it won't inherit the root logger's level. You're using debug in the packages you specify, not info. Specifying the level overrides whatever has been inherited.

If you want to inherit the root logger's level, get rid of the level specification in your logger configurations.

追风人 2024-09-12 06:30:36

要检索 rootlogger,您是否使用 Logger.getRootLogger() ?如果没有,您可能无法获得真正的根记录器。如果是这样,请确保 stdout 的阈值不在调试状态;附加程序的阈值覆盖记录器级别的阈值。

To retrieve the rootlogger, are you using Logger.getRootLogger()? If not, you may not be getting the real root logger. If so, make sure the Threshold of stdout isnt at debug; the Threshold of appenders override that of the logger Levels.

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