2

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?

1 Answer 1

10

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.