所以,我正在创建一个自定义的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 conf中读取更复杂的参数,我在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>
答案 0 :(得分:1)
为了让Logback在其域中的自定义对象上填充复杂的集合类型,必须满足以下条件:
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 "...";
}
}
...并为MyComplexArray
和MyComplexElement
提供以下类定义(注意:这些类的名称必须与您在{{中使用的元素名称相匹配) 1}}):
logback.xml
如果这太尴尬了,那么你可能想要回到一个简单的String属性(就像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;
}
}
)并传入一个编码的字符串,从中导出<mySimpleArg>
中的“复杂对象” 。例如:
MyCustomLayout