当Slf4j + log4j2存在MDC值时,如何使日志静音?

时间:2019-07-28 19:02:20

标签: java log4j2 slf4j

使用DynamicThresholdFilter可以找到的所有示例都与存在ThreadContext / MDC属性时记录 more 有关。但是我想做相反的事情,我运气不好。我什至调试了log4j2代码,这似乎是设计使然,但后来我不知道如何配置此方案。

car.logParts(); // logging visible
ThreadContext.put("mode", "simulation"); // prefer to use SLF4J's MDC
car.logParts(); // logging invisible
ThreadContext.clearAll();
car.logParts(); // logging visible again

从DynamicThresholdFilter中获得任何效果的唯一方法是在不匹配时DENY

<?xml version="1.0" encoding="UTF-8"?>
<!--suppress XmlUnboundNsPrefix -->
<configuration status="INFO">
    <DynamicThresholdFilter key="mode" onMatch="ACCEPT" onMismatch="DENY">
        <!-- does nothing no matter what I do-->
        <KeyValuePair key="simulation" value="error"/>
    </DynamicThresholdFilter>
    <appenders>
        <Console name="console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{HH:mm:ss} [%t] mode=[%X{mode}] %-5level %c{1} - %msg%n" />
        </Console>
    </appenders>
    <Loggers>
        <Root level="info">
            <AppenderRef ref="console" />
        </Root>
    </Loggers>
</configuration>

如何配置符合我要求的过滤器?

0 个答案:

没有答案