There are two ways you could accomplish what you want. (1) Create a CompositeFilter
as you mentioned in your question using both the RegexFilter
and ThreadContextMapFilter
. (2) Use a ScriptFilter
.
Below is some sample code to illustrate how you can do both of these.
First, a class to generate some log events:
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.
}
}
Now the log4j2.xml configuration file:
<?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>
Here is sample console output:
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
Notice that all of the messages are accepted since the console appender has no filters.
The logs written to File1 and File2 are identical, sample output follows:
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
Notice the 1 message where the regex and context value match has been excluded as desired.