简体   繁体   中英

log4j2 marker and isEnabled

I am trying to determine if a logger exists for a Marker in Log4j 2. The filter works when actually logging. The problem is when checking isEnabled(level, marker). If the MakerFilters are at the Configuration level, then it works as expected. If at the logger or appender level, then it bypasses the marker and considers only the threshold level.

Looking at the documentation, it looks like if accepted/denied is at the configuration level, then it will not check any other filters and that status will be honored, so that makes sense that it works here.

What I am trying to figure out is how to configure it at the Logger level so that Logger.isEnabled(Level,Marker) responds the same as when calling Logger.log(Level,Marker,msg).
Is there a way to configure this or is it the expected behavior that the two methods respond differently?

Tests run using: Log4j 2.0-rc1

Thanks

TestLog2.java

Here the logging statements that check each Marker for being enabled. DB_OP is a parent marker of DB_DELETE and 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 Config

Config with marker filters at logger level. Set to deny DB_DELETE, but accept DB_OP (includes 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>

Output

isEnabled returns true for all Marker queries. DB_DELETE log entry is not recorded.

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 Config

Now with the Filter at the Configuration Level

<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>

Output

Now isEnabled returns false for DB_DELETE, but true for other DB_OP markers.

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 ] 

There is currently a discussion going on on the log4j-Dev mailing list about markers. Your input and use case may be very useful to the implementors. Could you join the mailing list with your question? Or perhaps raise a Jira with your example config & code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM