Log4J2日志级别优先级 [英] Log4J2 Log Level precedence

查看:968
本文介绍了Log4J2日志级别优先级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有log4j2.xml,其部分配置为:

I have the log4j2.xml which is partially configured as:

<Loggers>
    <Root level="debug" includeLocation="true">
        <AppenderRef ref="FileInfo" level="info" />
        <AppenderRef ref="FileDebug" level="debug" />
    </Root>

    <Logger name="com.mycompany.domain.XYZ" level="TRACE" />
    <Logger name="com.mycompany.domain.ABC" level="TRACE" />
</Loggers>

但是,TRACE消息没有出现在两个文件附加器(FileInfo/FileDebug)中.当我将FileDebug日志级别更改为"TRACE"时,就会出现TRACE消息.

However, the TRACE messages doesn't come in either of the File appenders (FileInfo/FileDebug). When I change the FileDebug log level = "TRACE", then TRACE messages come.

问题:可以在多个位置指定日志级别".这些级别的优先顺序是多少?我认为在Logger标签上定义的级别应该是最高的.

Question: There are multiple places to specify log 'level'. Whats the precedence order for these levels. I thought level defined at Logger tag should be highest.

推荐答案

您应该花时间阅读

You should take the time to read the log4j2 manual regarding the architecture of log4j2. In particular the sections on "LoggerConfig" and "Appender".

您描述的行为是由于您没有与com.mycompany.domain.XYZcom.mycompany.domain.ABC记录器相关联的附加程序,并且可加性的默认值为true.

The behavior you describe is due to the fact that you don't have any appenders associated with your com.mycompany.domain.XYZ and com.mycompany.domain.ABC loggers, and the default value of additivity is true.

当邮件到达com.mycompany...个记录器之一且为TRACE级时,它将被记录器接受并发送到该记录器的所有附加程序-由于没有该消息,因此这些记录器的附加程序.

When a message reaches one of the com.mycompany... loggers and is TRACE level it will be accepted by the logger and sent to all appenders of that logger - which is NONE since there are no appenders for these loggers.

下一步,由于默认情况下可加性为true,因此该消息会到达记录器层次结构中下一个祖先的附加器,在您的情况下,这是根目录.根记录器的级别不影响该消息是否已被接受,因为子记录器已经接受了该消息.但是,由于您设置的附加器级别比TRACE更具体,因此该消息仍不会出现在输出中,因此附加器都拒绝该消息.

Next, since additivity is true by default the message goes to the appenders of the next ancestor in the logger hierarchy, which is root in your case. The level of the root logger does not affect whether the message is accepted since it was already accepted by the child logger. However, the message still would not appear in the output since you have the appenders' levels set more specific than TRACE, so the appenders both reject the message.

这使我想到了关于优先级的问题.答案是记录器的级别优先于附加器的级别.如果消息被记录器拒绝,它将永远不会到达该记录器的添加者.如果消息被记录器接受,但是附加器的级别比消息级别更具体,附加器仍可以拒绝该消息.棘手的部分是,当可加性为true(默认情况下为c2)时,祖先记录器的追加器将被视为子记录器的追加器,如上所述.

This brings me to your question about precedence. The answer to which is that the logger's level takes precedence over the appender's level. If the message is rejected by the logger it will never reach the appenders of that logger. If the message is accepted by the logger it can still be rejected by the appender if the appender's level is more specific than the message level. The tricky part is that when additivity is true (which it is by default) the appenders of ancestor loggers are treated as appenders of the child logger as I explained above.

换句话说-由于TRACE是第二个最低特定级别,因此您需要将记录器和附加程序都设置为TRACE或更低的级别(例如ALL),才能查看这些消息出现在输出中.或者,您可以简单地将记录器设置为TRACE或更低的级别,而在附加程序上设置该级别.

In other words - since TRACE is the second least specific level you would need to set both your logger and your appender to a level that is TRACE or lower (e.g. ALL) in order to see those messages appear in the output. Alternatively you can simply set your logger to a level of TRACE or lower and do not set the level on the appender.

这篇关于Log4J2日志级别优先级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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