使用复杂属性记录自定义布局

时间:2017-10-19 14:54:25

标签: java layout 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 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>

1 个答案:

答案 0 :(得分:1)

为了让Logback在其域中的自定义对象上填充复杂的集合类型,必须满足以下条件:

  • Logback必须能够使用简单的XML元素名称发现复杂对象 - &gt;类名映射
  • 复杂对象必须公开'加法器方法',例如如果复杂类型被称为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提供以下类定义(注意:这些类的名称必须与您在{{中使用的元素名称相匹配) 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