2017-02-09 49 views
1

我想從代碼中的特定位置登錄時限制異常堆棧跟蹤深度。繼http://logging.apache.org/log4j/2.x/manual/layouts.html#Pattern_Selectors我的配置是這樣的(關鍵部分):爲什麼在Slf4j + Log4j2中這個MarkerPatternSelector配置不起作用?

<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

我同意你的分析,也不能在配置中發現任何問題。 (感謝您顯示狀態日誌片段。)

您可能已找到缺陷。請在Log4j2 JIRA問題跟蹤器上提出此問題。請提及您正在使用的Log4j2版本。 (如果你還沒有試過最新版本,請先試試。)

+0

你說得對。我使用的是Log4j 2.4,升級到2.6後問題就消失了。謝謝! – alexandroid

+0

你可能想升級到2.8,因爲許多錯誤已經被修復,因爲2.6 ... :-) –