为什么Log4j rootLogger没有根据事件级别过滤日志事件? [英] Why is Log4j rootLogger not filtering log events according to event level?

查看:78
本文介绍了为什么Log4j rootLogger没有根据事件级别过滤日志事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么 Log4j rootLogger 在我的应用程序中没有根据级别过滤日志事件?在我的 log4j.properties 中,我有几个记录器:

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

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

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?

推荐答案

请参阅回答类似问题关于Log4j中的记录器链接:

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


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!

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

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.

因此,在这种情况下,有两种方法可以阻止root logger从另一个中捕获事件记录仪。第一种是禁用日志事件链的更具选择性的方法:

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

第二种方式更简单,但限制性更强,因为它会抑制控制台上的所有事件低于 INFO DEBUG TRACE ):

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

这篇关于为什么Log4j rootLogger没有根据事件级别过滤日志事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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