为什么 Log4j rootLogger 不根据事件级别过滤日志事件?
为什么我的应用程序中的 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
及更高级别的事件(WARN
、ERROR
和 FATAL
),这也是我想要的。为什么 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
请参阅此答案 类似的问题 Log4j 中的记录器链接:
这意味着无论您将根记录器的阈值设置为什么级别,它都将始终接受并输出任何其他记录器接受的日志事件,除非您禁用该子记录器的链接或显式将其附加程序的阈值设置为更高的水平。
因此,在这种情况下,有两种方法可以阻止根记录器捕获来自其他记录器的事件。第一种是更有选择性的禁用日志事件链接的方法:
第二种方法更简单,但限制性更强,因为它会抑制控制台上低于
INFO
(DEBUG 和
TRACE
):See this answer to a similar question about logger chaining in Log4j:
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:
The second way is simpler, but more restrictive since it suppresses all events on the console that are lower than
INFO
(DEBUG
andTRACE
):查看简介中描述的继承。如果您在包级别指定级别,它将不会继承根记录器的级别。您在指定的包中使用调试,而不是信息。指定级别会覆盖继承的任何内容。
如果您想继承根记录器的级别,请删除记录器配置中的级别规范。
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.
要检索 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 ofstdout
isnt at debug; the Threshold of appenders override that of the logger Levels.