log4j 佔位符%c{1}說明

%c{參數}或%logger{參數}

 

輸出logger的名稱,即語句private static final Logger logger = LogManager.getLogger(App.class.getName())中App.class.getName()的值。也可以使用其他字符串。

 

pattern表達式

logger名稱

結果

%c{1}

org.apache.com.te.Foo

Foo

%c{2}

org.apache.com.te.Foo

te.Foo

%c{1.}

org.apache.com.te.Foo

o.a.c.t.Foo

%c{1.1.!}

org.apache.com.te.Foo

o.a.!.!.Foo

%c{.}

org.apache.com.te.Foo

….Foo

 

 

1:如果{}內是正整數 N 那麼表示,將類名使用.分割,保留右邊數N個。0是正整數  至少會保留類名

2:如果{}內是負整數 N 那麼表示,將類名使用.分割,刪除左邊數N個。

3:使用.分割N個區域,同時將類目也使用.分割M個區域   N[0]處理M[0]  依次類推  如果N短  那麼 N[最後一個]  處理  M[所有沒有對應的]

對於每一個N的區域,都需要初始化兩個變量,charCount也就是對應M區域內保留的字符串,ellipsis表示分隔符,注意'\0'表示刪除

邏輯如下

第一個字符是* charCount = Integer.MAX_VALUE; ellipsis=下一個字符
第一個字符是數字 charCount = trimmed.charAt(pos) - '0';也就是數字的值 ellipsis=下一個字符
其他情況是0 ellipsis=當前字符
如果 ellipsis='.' 那麼 ellipsis = '\0'; 表示刪除
 
原始
org.apache.logging.log4j.core.layout.PatternLayoutTest Level=INFO Message=Hello, world 1!
 
配置 %c{1.2A.3.5.} %m
輸出 o.apA.log.log4j.core.layou.PatternLayoutTest Hello, world 1!
 
%c{1T.2A.3K.5M.1F.2J} %m
oT.apA.logK.log4j.cF.laJ.PatternLayoutTest Hello, world 1!
 
%c{1T} %m
oT.aT.lT.lT.cT.lT.PatternLayoutTest Hello, world 1!
 
%c{1.} %m
o.a.l.l.c.l.PatternLayoutTest Hello, world 1!
 
 
%c{1.2.} %m
o.ap.lo.lo.co.la.PatternLayoutTest Hello, world 1!
 
%c{1.2.1J} %m
o.ap.lJ.lJ.cJ.lJ.PatternLayoutTest Hello, world 1!
 
%c{1.2.J} %m
o.ap.J.J.J.J.PatternLayoutTest Hello, world 1!
表示第三個區域 以及以後 保留0個字符 使用J分割
 
%c{1.2J..1M} %m
o.apJ..lM.cM.lM.PatternLayoutTest Hello, world 1!
第三個區域 保留0個字符 沒有分割(也就是刪除了)
第四個以及以後的,保留1個字符 M分割
 

 

 

相關源碼參考

org.apache.logging.log4j.core.pattern.NameAbbreviator#getAbbreviator

/**
 * Gets an abbreviator.
 * <p>
 * For example, "%logger{2}" will output only 2 elements of the logger name, "%logger{1.}" will output only the
 * first character of the non-final elements in the name, "%logger(1~.2~} will output the first character of the
 * first element, two characters of the second and subsequent elements and will use a tilde to indicate abbreviated
 * characters.
 * </p>
 *
 * @param pattern
 *        abbreviation pattern.
 * @return abbreviator, will not be null.
 */
public static NameAbbreviator getAbbreviator(final String pattern) {
    if (pattern.length() > 0) {
        //  if pattern is just spaces and numbers then
        //     use MaxElementAbbreviator
        final String trimmed = pattern.trim();

        if (trimmed.isEmpty()) {
            return DEFAULT;
        }

        boolean isNegativeNumber;
        final String number;

        // check if number is a negative number
        if (trimmed.length() > 1 && trimmed.charAt(0) == '-') {
            isNegativeNumber = true;
            number = trimmed.substring(1);
        } else {
            isNegativeNumber = false;
            number = trimmed;
        }

        int i = 0;

        while (i < number.length() && number.charAt(i) >= '0'
                && number.charAt(i) <= '9') {
            i++;
        }

        //
        //  if all blanks and digits
        //
        if (i == number.length()) {
            return new MaxElementAbbreviator(Integer.parseInt(number),
                    isNegativeNumber? MaxElementAbbreviator.Strategy.DROP : MaxElementAbbreviator.Strategy.RETAIN);
        }

        final ArrayList<PatternAbbreviatorFragment> fragments = new ArrayList<>(5);
        char ellipsis;
        int charCount;
        int pos = 0;

        while (pos < trimmed.length() && pos >= 0) {
            int ellipsisPos = pos;

            if (trimmed.charAt(pos) == '*') {
                charCount = Integer.MAX_VALUE;
                ellipsisPos++;
            } else if (trimmed.charAt(pos) >= '0' && trimmed.charAt(pos) <= '9') {
                charCount = trimmed.charAt(pos) - '0';
                ellipsisPos++;
            } else {
                charCount = 0;
            }

            ellipsis = '\0';

            if (ellipsisPos < trimmed.length()) {
                ellipsis = trimmed.charAt(ellipsisPos);

                if (ellipsis == '.') {
                    ellipsis = '\0';
                }
            }

            fragments.add(new PatternAbbreviatorFragment(charCount, ellipsis));
            pos = trimmed.indexOf('.', pos);

            if (pos == -1) {
                break;
            }

            pos++;
        }

        return new PatternAbbreviator(fragments);
    }

    //
    //  no matching abbreviation, return defaultAbbreviator
    //
    return DEFAULT;
}

 

 

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章