alexandroid
alexandroid

Reputation: 1638

Why this MarkerPatternSelector config in Slf4j+Log4j2 does not work?

I am trying to limit exception stacktrace depth when logged from a specific place in code. Following http://logging.apache.org/log4j/2.x/manual/layouts.html#Pattern_Selectors my configuration looks like this (key part):

<PatternLayout>
    <MarkerPatternSelector defaultPattern="%d{dd MMM yyyy HH:mm:ss,SSS}: %m%n">
        <PatternMatch key="ExceptionInterceptor"
                      pattern="%d{dd MMM yyyy HH:mm:ss,SSS}: %m%throwable{5}%n" />
    </MarkerPatternSelector>
</PatternLayout>

In the Java code:

@Slf4j
public final class ExceptionInterceptor {
   private static final Marker MARKER = MarkerFactory.getMarker("ExceptionInterceptor");

   public void intercept(Throwable t) {
      log.info(MARKER, "Got exception", t);
   }

And yet for some reason the marker does not seem to be matched, i.e. I am getting full stack logged. When I add %throwable{5} to the defaultPattern attribute, the stack is truncated as expected.

Here is the relevant DEBUG log during the startup (stripped timestamps & level). If I read it correctly, it seems to construct the matcher ok.

Building Plugin[name=PatternMatch, class=org.apache.logging.log4j.core.layout.PatternMatch]. Searching for builder factory method...
Found builder factory method [newBuilder]: public static org.apache.logging.log4j.core.layout.PatternMatch$Builder org.apache.logging.log4j.core.layout.PatternMatch.newBuilder().
Calling build() on class class org.apache.logging.log4j.core.layout.PatternMatch$Builder for element PatternMatch with params(, key="ExceptionInterceptor", pattern="%d{dd MMM yyyy HH:mm:ss,SSS}: %m%throwable{5}%n")
Built Plugin[name=PatternMatch] OK from builder factory method.
Building Plugin[name=patternSelector, class=org.apache.logging.log4j.core.layout.MarkerPatternSelector]. Searching for builder factory method...
No builder factory method found in class org.apache.logging.log4j.core.layout.MarkerPatternSelector. Going to try finding a factory method instead.
Still building Plugin[name=patternSelector, class=org.apache.logging.log4j.core.layout.MarkerPatternSelector]. Searching for factory method...
Found factory method [createSelector]: public static org.apache.logging.log4j.core.layout.MarkerPatternSelector org.apache.logging.
log4j.core.layout.MarkerPatternSelector.createSelector(org.apache.logging.log4j.core.layout.PatternMatch[],java.lang.String,boolean,boolean,org.apache.logging.log4j.core.config.Configuration).

What is wrong with this code above which results in marker not being matched?

Upvotes: 2

Views: 902

Answers (1)

Remko Popma
Remko Popma

Reputation: 36754

I agree with your analysis and also can't find any issue with the configuration. (Thanks for showing the status log snippet.)

You may have found a bug. Please raise this on the Log4j2 JIRA issue tracker. Please mention the Log4j2 version you are using. (And if you haven't tried with the latest version, try that first. )

Upvotes: 1

Related Questions