为什么在Slf4j + Log4j2中这个MarkerPatternSelector配置不起作用?

时间:2017-02-09 05:51:45

标签: java logging slf4j log4j2

我试图在从代码中的特定位置记录时限制异常堆栈跟踪深度。在http://logging.apache.org/log4j/2.x/manual/layouts.html#Pattern_Selectors之后,我的配置如下所示(关键部分):

<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>

在Java代码中:

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

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

但由于某些原因,标记似乎没有匹配,即我正在获得完整的堆栈记录。当我将%throwable{5}添加到defaultPattern属性时,堆栈会按预期被截断。

这是启动期间的相关DEBUG日志(剥离的时间戳和级别)。如果我正确阅读它,似乎构建匹配器好。

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).

上面的代码有什么问题导致标记不匹配?

1 个答案:

答案 0 :(得分:1)

我同意您的分析,也无法找到配置问题。 (感谢您显示状态日志片段。)

您可能发现了一个错误。请在Log4j2 JIRA问题跟踪器上提出此问题。请提及您正在使用的Log4j2版本。 (如果您还没有尝试使用最新版本,请先试试。)