IbrahimMitko
IbrahimMitko

Reputation: 1207

Log4j2 - Filter the logs based on the pattern

I have the following layout pattern in my log4j2 xml file:

<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36}:%L [%X{**FILTER_ON_ME**}] - %msg%n</pattern>

What I'm looking for is a way to filter log messages to certain files based on the value of FILTER_ON_ME. However it seems like most of the log4j2 filters are specifically focused on the %msg and not the pattern.

Is there a way this can be done?

EG.

<Root level="ERROR">
   <AppenderRef ref="ConsoleAppender"/>
   <AppenderRef ref="RollingFileAppender">
       <StringMatchFilter text="**FILTER_ON_ME_VALUE**" onMatch="DENY" onMismatch="NEUTRAL"/>
       <StringMatchFilter text="**FILTER_ON_ME_VALUE**" onMatch="DENY" onMismatch="NEUTRAL"/>
   </AppenderRef>
   <AppenderRef ref="RollingFileAppender2">
        <StringMatchFilter text="**FILTER_ON_ME_VALUE**" onMatch="ACCEPT" onMismatch="NEUTRAL"/>
   </AppenderRef>
   <AppenderRef ref="RollingFileAppender3">
        <StringMatchFilter text="**FILTER_ON_ME_VALUE**" onMatch="ACCEPT" onMismatch="NEUTRAL"/>
  </AppenderRef>
</Root>

Upvotes: 0

Views: 41

Answers (0)

Related Questions