log4j2如何过滤掉没有ThreadContext键的消息

时间:2019-02-06 14:59:20

标签: filter log4j2

在我的应用程序中,有时在登录时有一个threadContext键'jobId',有时却没有。

因此,仅当threadContext中存在键“ jobId”时,我才希望将日志消息传递到我的jdbc日志附加程序(需要字段“ jobId”)。

我无法管理它来配置DynamicThresholdFilter,从而跳过日志消息

<JDBC name="databaseAppender" tableName="LOG_MESSAGE">
        <DynamicThresholdFilter key="jobId" defaultThreshold="INFO" onMatch="ACCEPT" onMismatch="DENY">
        </DynamicThresholdFilter>
        <DataSource jndiName="java:jboss/datasources/myDS" />
        <Column name="MESSAGE" pattern="%message" />
        <Column name="Job_KEY_" pattern="%X{jobId}" />
    </JDBC>

1 个答案:

答案 0 :(得分:1)

您可以使用RoutingAppender来执行此操作,类似于example上的log4j2 FAQ page

这是一个进行记录的Java类:

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.ThreadContext;

public class SomeClass {

    private static final Logger log = LogManager.getLogger();

    public static void main(String[] args){

        //These should appear in the console only
        log.debug("No jobId, This is some debug!");
        log.info("No jobId, Here's some info!");
        log.error("No jobId, Some error happened!");

        //These should route to a simulated JDBC appender only
        ThreadContext.put("jobId", "1234");
        log.debug("With jobId, This is some debug!");
        log.info("With jobId, Here's some info!");
        log.error("With jobId, Some error happened!");

        //Once again these will go to console only
        ThreadContext.remove("jobId");
        log.debug("No jobId, This is some debug!");
        log.info("No jobId, Here's some info!");
        log.error("No jobId, Some error happened!");
    }
}

这是一个示例log4j2.xml配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
        </Console>

        <File name="simulatedJdbc" fileName="logs/simulatedJdbc.txt"
            immediateFlush="false" append="true">
            <PatternLayout
                pattern="%d{yyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} %X{jobId} - %msg%n" />
        </File>


        <Routing name="Routing">
            <Routes pattern="$${ctx:jobId}">
                <!-- This route is chosen if ThreadContext has no value for key jobId. -->
                <Route key="$${ctx:jobId}" ref="Console" />
                <!-- This is the default route when no others match -->
                <Route ref = "simulatedJdbc"/>              
            </Routes>
        </Routing>
    </Appenders>

    <Loggers>
        <Root level="debug">
            <AppenderRef ref="Routing" />
        </Root>
    </Loggers>
</Configuration>

运行上面的结果是以下输出:

控制台输出:

17:49:41.659 [main] DEBUG example.SomeClass - No jobId, This is some debug!
17:49:41.663 [main] INFO  example.SomeClass - No jobId, Here's some info!
17:49:41.664 [main] ERROR example.SomeClass - No jobId, Some error happened!
17:49:41.664 [main] DEBUG example.SomeClass - No jobId, This is some debug!
17:49:41.664 [main] INFO  example.SomeClass - No jobId, Here's some info!
17:49:41.664 [main] ERROR example.SomeClass - No jobId, Some error happened!

simulatedJdbc.txt输出:

2019-02-08 17:49:41.664 [main] DEBUG example.SomeClass 1234 - With jobId, This is some debug!
2019-02-08 17:49:41.664 [main] INFO  example.SomeClass 1234 - With jobId, Here's some info!
2019-02-08 17:49:41.664 [main] ERROR example.SomeClass 1234 - With jobId, Some error happened!

如您所见,当ThreadContext包含jobId时,日志将路由到模拟的JDBC附加程序(在我的示例中只是一个文件附加程序)。当jobId丢失时,日志将路由到控制台附加程序。

希望这会有所帮助!