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>
哇,謝謝你的答案。他們肯定應該把它寫入文檔。 – Kamil