0

我想在我的自定义 logback 布局中注入一些静态标签。

Map<String, String>logback.xml中定义 a 的正确语法是什么?

基本logback.xml

<configuration>
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
            <layout class="com.example.CustomEncoder">
                <!-- What I've tried -->
                <labels>
                    <key>qwe</key>
                    <value>asd</value>
                </labels>
                <pattern>%message %xException{10}</pattern>
            </layout>
        </encoder>
    </appender>

    <root level="INFO">
        <appender-ref ref="STDOUT"/>
    </root>
</configuration>

自定义编码器.java

package com.example;

import ch.qos.logback.classic.PatternLayout;
import ch.qos.logback.classic.spi.ILoggingEvent;

import java.util.Map;

public class Encoder extends PatternLayout {
    
    private Map<String, String> labels;
    
    public void setLabels(Map<String, String> labels) {
        this.labels = labels;
    }
    
    @Override
    public String doLayout(ILoggingEvent event) {
        // do something with labels
        return super.doLayout(event);
    }
}

4

0 回答 0