这是我的 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 值和 CharacterEscapesJsonFactoryDecorator。请你帮我解决这个问题。
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>