Using multiple markers in Log4j 2 to filter logging

java, log4j2, logging

Solution

Set the first two `onMismatch=NEUTRAL` and keep the last one as `onMismatch=DENY`. That way 2nd and 3rd aren't automatically denied after processing the first.

Problem

I'm trying to set up my log4j logger to use multiple `Marker`'s when filtering what to log. I'm not entirely sure if I'm even on the right track, but basically I want is to be able to log a number of different events based on a number of different markers. Suppose I have three markers, `MARKERONE`, `MARKERTWO` and `MARKERTHREE`. These three are all "top-level" markers, meaning they cannot enherit from either of the other markers. Currently I've got the following `log4j.xml` configuration: ``` <?xml version="1.0" encoding="UTF-8"?> <Configuration status="warn" name="MyApp" packages=""> <Appenders> <RollingFile name="RollingFile" fileName="logs/app.log" filePattern="logs/app-%d{MM-dd-yyyy}.log.gz"> <MarkerFilter marker="MARKERONE" onMatch="ACCEPT" onMismatch="DENY"/> <MarkerFilter marker="MARKERTWO" onMatch="ACCEPT" onMismatch="DENY"/> <MarkerFilter marker="MARKERTHREE" onMatch="ACCEPT" onMismatch="DENY"/> <PatternLayout> <pattern>%d %p %c{1.} [%t] %m%n</pattern> </PatternLayout> <TimeBasedTriggeringPolicy /> </RollingFile> </Appenders> <Loggers> <Root level="error"> <AppenderRef ref="RollingFile"/> </Root> </Loggers> </Configuration> ``` This is based on the example xml-file from the log4j website: http://logging.apache.org/log4j/2.x/manual/filters.html#MarkerFilter This doesn't work, however, because of errors. I then tried replacing the three `MarkerFilter` entries with the following: ``` <Filters> <MarkerFilter marker="MARKERONE" onMatch="ACCEPT" onMismatch="DENY"/> <MarkerFilter marker="MARKERTWO" onMatch="ACCEPT" onMismatch="DENY"/> <MarkerFilter marker="MARKERTHREE" onMatch="ACCEPT" onMismatch="DENY"/> </Filters> ``` This allowed me to run the logging tool and actually log stuff, but (!) I am only able to log events logged with `MARKERONE`. If I change the order of the `MarkerFilter`'s, only events using the first `MarkerFilter` will be logged. Ultimately my goal is to be able to specify exactly what I would like to log (in the xml-file for example), eg.: "I would like to log `MARKERTWO` and `MARKERTHREE`, but not `MARKERONE`". I'm thinking there has to be a way for me to log whatever I want to log, without being limited to one `Marker`. Isn't this so? How do I log using multiple markers?

Original source