如何在 spring 应用程序的 Logback 中以编程方式传递 Patten 和 JsonFactoryDecorator
How to pass the Patten and JsonFactoryDecorator programmatically in Logback in spring application
这是我的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>
由于我使用的是具有相同编码模式的不同附加程序,并且为了重用该模式,我正尝试以编程方式添加,如下所示,但在这里我不确定如何从 class级。请你帮我解决这个问题。
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>
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();
}
}
这是我的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>
由于我使用的是具有相同编码模式的不同附加程序,并且为了重用该模式,我正尝试以编程方式添加,如下所示,但在这里我不确定如何从 class级。请你帮我解决这个问题。
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>
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();
}
}