2017-10-19 92 views
0

所以,我創建基於自定義的logback佈局:複雜的logback自定義佈局屬性

public class MyCustomLayout extends LayoutBase<ILoggingEvent> { 
    private String mySimpleArg; 

    public void setMySimpleArg(String mySimpleArg) { 
     this.mySimpleArg = mySimpleArg; 
    } 

    public String doLayout(IlogginEvent iLoggingEvent) { 
     //generate log in my format and use mySimpleArg 
    } 
} 

,在我logback.xml我有

<appender ...> 
    <encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder"> 
     <layout class="path.to.MyCustomLayout"> 
      <mySimpleArg>some text</mySimpleArg> 
     </layout> 
    </encoder> 
... 
</appender> 

現在的問題是,我也需要從我的XML配置文件中讀取更復雜的參數,並且我無法在logback文檔和Google上找到任何信息。具體而言,我需要像

public class MyCustomLayout extends LayoutBase<ILoggingEvent> { 
    private String mySimpleArg; 
    //the Pair here is just to show I need something key -> value based 
    private Pair<String, Object>[] myComplexArray; 

    public void setMySimpleArg(String mySimpleArg) { 
     this.mySimpleArg = mySimpleArg; 
    } 

    public void setMyComplexArray(Pair<String, Object>[] myComplexArray) { 
     this.myComplexArray = myComplexArray; 
    } 

    public String doLayout(IlogginEvent iLoggingEvent) { 
     //generate log in my format and use mySimpleArg and myComplexArray 
    } 
} 

和logback.xml

<appender ...> 
    <encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder"> 
     <layout class="path.to.MyCustomLayout"> 
      <mySimpleArg>some text</mySimpleArg> 
      <myComplexArray> 
       <myComplexElement key="key1" value="value1"/> 
       <myComplexElement key="key2" value=5/> 
      </myComplexArray> 
     </layout> 
    </encoder> 
... 
</appender> 

回答

1

爲了要的logback在其領域的自定義對象下面必須持有正確的填充複雜,集合類型:

  • 的logback必須能夠發現使用一個簡單的XML元素名稱的複雜對象 - >類名稱映射
  • 複雜OBJ ect必須公開「加法方法」,例如如果複雜類型被稱爲Foo,它包含的Bar集合然後Foo必須公開public addBar(Bar bar){}方法。

考慮到這一點,下面的配置...

<encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder"> 
    <layout class="path.to.MyCustomLayout"> 
     <mySimpleArg>some text</mySimpleArg> 
     <myComplexArray> 
      <myComplexElement> 
       <key>key1</key> 
       <value>value1</value> 
      </myComplexElement> 
      <myComplexElement> 
       <key>key2</key> 
       <value>value2</value> 
      </myComplexElement> 
     </myComplexArray> 
    </layout> 
</encoder> 

...只要你聲明MyCustomLayout像這樣將工作...

public class MyCustomLayout extends LayoutBase<ILoggingEvent> { 
    private String mySimpleArg; 
    private MyComplexArray myComplexArray; 

    public void setMySimpleArg(String mySimpleArg) { 
     this.mySimpleArg = mySimpleArg; 
    } 

    public void setMyComplexArray(MyComplexArray myComplexArray) { 
     this.myComplexArray = myComplexArray; 
    } 

    public String doLayout(ILoggingEvent iLoggingEvent) { 
     // generate log in my format and use mySimpleArg and myComplexArray 
     return "..."; 
    } 
} 

...你提供MyComplexArrayMyComplexElement以下類定義(注:這些類必須符合您在logback.xml使用元素名稱的名稱):

public class MyComplexArray { 

    private List<MyComplexElement> myComplexElements = new ArrayList<>(); 

    public MyComplexArray() { 
    } 

    public void addMyComplexElement(MyComplexElement myComplexElement) { 
     myComplexElements.add(myComplexElement); 
    } 
} 

public class MyComplexElement { 

    private String key; 
    private String value; 

    public MyComplexElement() { 
    } 

    public String getKey() { 
     return key; 
    } 

    public void setKey(String key) { 
     this.key = key; 
    } 

    public String getValue() { 
     return value; 
    } 

    public void setValue(String value) { 
     this.value = value; 
    } 
} 

如果這一切都太尷尬,那麼你可能要回落到一個簡單的String屬性(就像<mySimpleArg>),並通過在從中派生內MyCustomLayout「複雜對象的編碼字符串。例如:

<myComplexArg>foo=bar,bas=1|x=y,z=2</myComplexArg> 
+0

哇,謝謝你的答案。他們肯定應該把它寫入文檔。 – Kamil