0

这是我的 logback.spring.xml

<encoder class="net.logstash.logback.encoder.LoggingEventCompositeJsonEncoder">
            <providers>
                
                <arguments/>
               
                <stackTrace/>
               
                <mdc>
                    <excludeMdcKeyName>ClaimNo</excludeMdcKeyName>
                    <excludeMdcKeyName>ClaimId</excludeMdcKeyName>
                </mdc>
                <pattern>
                    <pattern>
                        {
                        "Claim": "[${logback.company}, ${logback.info}]",  // YML file value 
                        "Log level": "${logback.level}",    // YML value 
                        "Time": "%date",
                        "Thread ID": "%thread",
                        "claimId":"[%X{claimId}]",   //MDC key 
                        "Claim no": "[%X{CliamNo}]", // MDC Key
                        "Logger": "%logger",
                        "Message": "%msg"
                        }
                    </pattern>
                </pattern>
            </providers>
            <jsonFactoryDecorator class="net.logstash.logback.decorate.CharacterEscapesJsonFactoryDecorator">
                <escape>
                    <targetCharacterCode>9</targetCharacterCode> <!-- Tab-->
                    <escapeSequence></escapeSequence>
                </escape>
                <escape>
                    <targetCharacterCode>10</targetCharacterCode>  <!-- New Line-->
                    <escapeSequence></escapeSequence>
                </escape>
            </jsonFactoryDecorator>
        </encoder>

由于我使用具有相同编码模式的不同附加程序并且为了重用该模式,我试图以如下方式以编程方式添加,但在这里我不确定如何从类级别传递模式、MDC 值和 CharacterEscapesJsonFactoryDe​​corator。请你帮我解决这个问题。

public class JsonEncoder extends LoggingEventCompositeJsonEncoder {
    public JsonEncoder() {
    }
    @Override
    public void start() {
       

        List<String> mdcList = new ArrayList<String>();
        mdcList.add("ClaimNo");
        mdcList.add("CliamId");
        MdcJsonProvider mdcJsonProvider= new MdcJsonProvider();
        mdcJsonProvider.setExcludeMdcKeyNames(mdcList);

        JsonProviders<ILoggingEvent> providers = getProviders();
        providers.addProvider(mdcJsonProvider);
        
        super.start();
    }
}

Logback.spring.xml

<encoder class="com.solution.JsonEncoder"></encoder>
4

1 回答 1

2

xml 元素名称与底层配置类的 setter/adder 方法相匹配。

另请注意,模式值不应包含注释。

public class JsonEncoder extends LoggingEventCompositeJsonEncoder {

    @Override
    public void start() {
        /*
         * <providers>
         */
        JsonProviders<ILoggingEvent> providers = getProviders();
        providers.setContext(getContext());

        /*
         * <arguments/>
         */
        providers.addProvider(new ArgumentsJsonProvider());

        /*
         * <stackTrace/>
         */
        providers.addProvider(new StackTraceJsonProvider());


        /*
         * <mdc>
         *     <excludeMdcKeyName>ClaimNo</excludeMdcKeyName>
         *     <excludeMdcKeyName>ClaimId</excludeMdcKeyName>
         * </mdc>
         */
        MdcJsonProvider mdcJsonProvider= new MdcJsonProvider();
        List<String> mdcList = new ArrayList<String>();
        mdcList.add("ClaimNo");
        mdcList.add("ClaimId");
        mdcJsonProvider.setExcludeMdcKeyNames(mdcList);
        providers.addProvider(mdcJsonProvider);

        /*
         * <pattern>
         *     <pattern>...</pattern>
         * </pattern>
         */
        LoggingEventPatternJsonProvider patternJsonProvider = new LoggingEventPatternJsonProvider();
        patternJsonProvider.setPattern("{\n" +
                "\"Claim\": \"[${logback.company}, ${logback.info}]\",\n" +
                "\"Log level\": \"${logback.level}\",\n" +
                "\"Time\": \"%date\",\n" +
                "\"Thread ID\": \"%thread\",\n" +
                "\"claimId\":\"[%X{claimId}]\",\n" +
                "\"Claim no\": \"[%X{CliamNo}]\",\n" +
                "\"Logger\": \"%logger\",\n" +
                "\"Message\": \"%msg\"\n" +
                "}");
        providers.addProvider(patternJsonProvider);

        /*
         * </providers>
         */

        /*
         * <jsonFactoryDecorator class="net.logstash.logback.decorate.CharacterEscapesJsonFactoryDecorator">
         *     <escape>
         *         <targetCharacterCode>9</targetCharacterCode> <!-- Tab-->
         *         <escapeSequence></escapeSequence>
         *     </escape>
         *     <escape>
         *         <targetCharacterCode>10</targetCharacterCode>  <!-- New Line-->
         *         <escapeSequence></escapeSequence>
         *     </escape>
         * </jsonFactoryDecorator>
         */
        CharacterEscapesJsonFactoryDecorator jsonFactoryDecorator = new CharacterEscapesJsonFactoryDecorator();

        CharacterEscapesJsonFactoryDecorator.Escape escape1 = new CharacterEscapesJsonFactoryDecorator.Escape();
        escape1.setTargetCharacterCode(9);
        escape1.setEscapeSequence("");
        jsonFactoryDecorator.addEscape(escape1);

        CharacterEscapesJsonFactoryDecorator.Escape escape2 = new CharacterEscapesJsonFactoryDecorator.Escape();
        escape2.setTargetCharacterCode(10);
        escape2.setEscapeSequence("");
        jsonFactoryDecorator.addEscape(escape2);

        setJsonFactoryDecorator(jsonFactoryDecorator);

        super.start();
    }
}
于 2020-10-03T21:23:46.203 回答