log4j2标记和isEnabled

时间:2014-04-09 14:58:07

标签: java logging log4j2

我正在尝试确定Log4j中是否存在Marker的记录器。过滤器在实际记录时有效。问题是检查isEnabled(级别,标记)。如果MakerFilters处于配置级别,则它按预期工作。如果在记录器或追加器级别,则它绕过标记并仅考虑阈值级别。

查看文档,看起来如果接受/拒绝处于配置级别,那么它将不会检查任何其他过滤器,并且该状态将被尊重,因此这在其中有效。

我想弄清楚的是如何在Logger级别配置它,以便Logger.isEnabled(Level,Marker)响应与调用Logger.log(Level,Marker,msg)时相同。 有没有办法配置这个,还是两种方法的反应方式不同?

测试运行使用:Log4j 2.0-rc1

由于

TestLog2.java

此处用于检查每个Marker是否已启用的日志记录语句。 DB_OP是DB_DELETE和DB_QUERY的父标记。

private final static Logger log = LogManager.getLogger(TestLog2.class);
public int method2(String string, int i) {
    Level logLevel = Level.INFO;
    log.entry(string, i);
    int x = i + 2;

    System.out.printf("Level: %s is enabled: %s%n", logLevel, log.isEnabled(logLevel));
    // This should return false
    System.out.printf("Level: %s : DB_DELETE is enabled: %s%n", logLevel, log.isEnabled(logLevel, DbMarkers.DB_DELETE.getMarker()));
    System.out.printf("Level: %s : DB_QUERY is enabled: %s%n", logLevel, log.isEnabled(logLevel, DbMarkers.DB_QUERY.getMarker()));
    System.out.printf("Level: %s : DB_OP is enabled: %s%n", logLevel, log.isEnabled(logLevel, DbMarkers.DB_OP.getMarker()));

    log.log(logLevel, DbMarkers.DB_OP.getMarker(), "This is a parent db marker message");
    // This should not print 
    log.log(logLevel, DbMarkers.DB_DELETE.getMarker(), "This is a delete marker message");
    log.log(logLevel, DbMarkers.DB_QUERY.getMarker(), "This is a query marker message");

    log.exit(x);
    return x;
}

Log4j2配置

在记录器级别使用标记过滤器进行配置。设置为拒绝DB_DELETE,但接受DB_OP(包括DB_QUERY)

<Configuration status="error" strict="true" name="XMLConfigTest"
    packages="org.apache.logging.log4j.test">
    <Appenders>
        <Appender type="Console" name="DB">
            <Layout type="PatternLayout"
                pattern="DB: %d{HH:mm:ss.SSS} [%t] %-5level %C{1}.%M %m %marker %ex%n" />
        </Appender>
        <Appender type="Console" name="STDOUT">
            <Layout type="PatternLayout" pattern="STDOUT: %d{HH:mm:ss.SSS} [%t] %-5level %C{1}.%M %m %marker %ex%n" />
        </Appender>
    </Appenders>

    <Loggers>
        <Logger name="net.dt.log.test2" level="info" additivity="false">
            <AppenderRef ref="DB" />
            <Filters>
                <Filter type="MarkerFilter" marker="DB_DELETE" onMatch="DENY" onMismatch="NEUTRAL" /> 
                <Filter type="MarkerFilter" marker="DB_OP" onMatch="ACCEPT" onMismatch="DENY" />
            </Filters>
        </Logger>
        <Root level="info">
            <AppenderRef ref="STDOUT">
                <Filter type="MarkerFilter" marker="DB_OP" onMatch="DENY" onMismatch="NEUTRAL" />
            </AppenderRef>
        </Root>
    </Loggers>

</Configuration>

输出

isEnabled对所有Marker查询都返回true。 不记录DB_DELETE日志条目。

STDOUT: 15:00:13.655 [main] ERROR TestLog1.method1 An error in TestLog1 : Should see this  
Level: INFO is enabled: true
Level: INFO : DB_DELETE is enabled: true  <---- This should be false
Level: INFO : DB_QUERY is enabled: true
Level: INFO : DB_OP is enabled: true
DB: 15:00:13.659 [main] INFO  TestLog2.method2 This is a parent db marker message DB_OP 
--- DB_DELETE log entry would be here if it was configured to accept. 
DB: 15:00:13.660 [main] INFO  TestLog2.method2 This is a query marker message DB_QUERY[ DB_OP ] 

Log4j2配置

现在使用配置级别的过滤器

<Configuration status="error" strict="true" name="XMLConfigTest"
    packages="org.apache.logging.log4j.test">
    <Filters> 
        <Filter type="MarkerFilter" marker="DB_DELETE" onMatch="DENY" onMismatch="NEUTRAL" /> 
        <Filter type="MarkerFilter" marker="DB_OP" onMatch="ACCEPT" onMismatch="NEUTRAL" /> 
    </Filters> 
    <Appenders>
        <Appender type="Console" name="DB">
            <Layout type="PatternLayout"
                pattern="DB: %d{HH:mm:ss.SSS} [%t] %-5level %C{1}.%M %m %marker %ex%n" />
        </Appender>
        <Appender type="Console" name="STDOUT">
            <Layout type="PatternLayout" pattern="STDOUT: %d{HH:mm:ss.SSS} [%t] %-5level %C{1}.%M %m %marker %ex%n" />
        </Appender>
    </Appenders>

    <Loggers>
        <Logger name="net.dt.log.test2" level="info" additivity="false">
            <AppenderRef ref="DB" />
            <Filters>
                <Filter type="MarkerFilter" marker="DB_DELETE" onMatch="DENY" onMismatch="NEUTRAL" /> 
                <Filter type="MarkerFilter" marker="DB_OP" onMatch="ACCEPT" onMismatch="DENY" />
            </Filters>
        </Logger>
        <Root level="info">
            <AppenderRef ref="STDOUT">
                <Filter type="MarkerFilter" marker="DB_OP" onMatch="DENY" onMismatch="NEUTRAL" />
            </AppenderRef>
        </Root>
    </Loggers>

输出

现在isEnabled为DB_DELETE返回false,但对于其他DB_OP标记则为true。

STDOUT: 15:24:33.565 [main] ERROR TestLog1.method1 An error in TestLog1 : Should see this  
Level: INFO is enabled: true
Level: INFO : DB_DELETE is enabled: false  <----- Now it shows as false
Level: INFO : DB_QUERY is enabled: true
Level: INFO : DB_OP is enabled: true
DB: 15:24:33.570 [main] INFO  TestLog2.method2 This is a parent db marker message DB_OP 
DB: 15:24:33.570 [main] INFO  TestLog2.method2 This is a query marker message DB_QUERY[ DB_OP ] 

1 个答案:

答案 0 :(得分:1)

目前有关于标记的log4j-Dev邮件列表正在进行讨论。您的输入和用例可能对实现者非常有用。你能和你的问题一起加入邮件列表吗?或者也许用您的示例配置和放大器来提升Jira;码。