过滤掉Log4j2中匹配两个过滤器的消息



我有一个正在运行的Java应用程序,它使用log4j2.xml配置日志记录。我想删除消息中包含特定字符串的消息也有一定的MDC键。

如果我像这样在MDC上指定一个过滤器:

<ThreadContextMapFilter onMatch="DENY" onMismatch="NEUTRAL">
<KeyValuePair key="someValue" value="foo"/>
</ThreadContextMapFilter>

它正确地删除具有此键值对的所有消息,但我需要它仅在RegexFilter也匹配时删除这些消息。有什么办法可以做到吗?我看到有CompositeFilter,但据我所知,它不能为我执行这种布尔运算。

我没有能力修改这个应用程序的源代码,我所要做的就是log4j2.xml

有两种方法可以实现您想要的。(1)使用RegexFilterThreadContextMapFilter创建一个CompositeFilter,正如你在问题中提到的那样。(2)使用ScriptFilter.

下面是一些示例代码,说明如何实现这两种操作。首先,生成一些日志事件的类:

package example;
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){
ThreadContext.put("someValue", "foo");
log.info("Here's a test of info!"); //should not be accepted
log.info("A message that doesn't match the regex"); //should be accepted
ThreadContext.put("someValue", "bar");
log.info("Another test of info"); //regex matches, context does not. Accepted.
log.info("Some other message"); //does not match regex, context does not match. Accepted.
}
}

现在log4j2.xml配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{yyy-MM-dd} [%t] %-5level %logger{36} %X{someValue} - %msg%n" />
</Console>
<File name="File1" fileName="logs/File1.log" immediateFlush="false"
append="false">
<PatternLayout
pattern="%d{yyy-MM-dd} [%t] %-5level %logger{36} %X{someValue} - %msg%n" />
<Filters>
<ThreadContextMapFilter onMatch="NEUTRAL" onMismatch="ACCEPT">
<KeyValuePair key="someValue" value="foo"/>
</ThreadContextMapFilter>
<RegexFilter regex=".* test .*" onMatch="DENY" onMismatch="NEUTRAL"/>
</Filters>
</File>
<File name="File2" fileName="logs/File2.log" immediateFlush="false"
append="false">
<PatternLayout
pattern="%d{yyy-MM-dd} [%t] %-5level %logger{36} %X{someValue} - %msg%n" />
<ScriptFilter onMatch="ACCEPT" onMisMatch="DENY">
<Script name="MyFilter" language="JavaScript"><![CDATA[
if(logEvent.getContextMap().get("someValue") == "foo" 
&& logEvent.getMessage().getFormattedMessage().matches(".* test .*")){
false
}else{
true
}
]]></Script>
</ScriptFilter>
</File>
</Appenders>
<Loggers>
<Root level="ALL">
<AppenderRef ref="Console"  />
<AppenderRef ref="File1"  />
<AppenderRef ref="File2"  />
</Root>
</Loggers>
</Configuration>

下面是控制台输出的示例:

2021-09-24 [main] INFO  example.SomeClass foo - Here's a test of info!
2021-09-24 [main] INFO  example.SomeClass foo - A message that doesn't match the regex
2021-09-24 [main] INFO  example.SomeClass bar - Another test of info
2021-09-24 [main] INFO  example.SomeClass bar - Some other message

注意,所有消息都被接受,因为控制台附加程序没有过滤器。

写入File1和File2的日志相同,示例输出如下:

2021-09-24 [main] INFO  example.SomeClass foo - A message that doesn't match the regex
2021-09-24 [main] INFO  example.SomeClass bar - Another test of info
2021-09-24 [main] INFO  example.SomeClass bar - Some other message

请注意,在1条消息中,regex和上下文值匹配已按预期排除。

最新更新