Java Slf4j,logback-从json中删除mdc标记
我正在使用MDC将上下文信息添加到日志记录中:Java Slf4j,logback-从json中删除mdc标记,java,logback,mdc,Java,Logback,Mdc,我正在使用MDC将上下文信息添加到日志记录中: MDC.put("Correlation-ID", UUID.randomUUID().toString()); 我正在使用以下logback编码器: <encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder"> <layout class="ch.qos.logback.contrib.json.classic.JsonLayo
MDC.put("Correlation-ID", UUID.randomUUID().toString());
我正在使用以下logback编码器:
<encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
<layout class="ch.qos.logback.contrib.json.classic.JsonLayout">
<timestampFormat>yyyy-MM-dd'T'HH:mm:ss.SSSX</timestampFormat>
<timestampFormatTimezoneId>Etc/UTC</timestampFormatTimezoneId>
<appendLineSeparator>true</appendLineSeparator>
<jsonFormatter class="ch.qos.logback.contrib.jackson.JacksonJsonFormatter">
<prettyPrint>false</prettyPrint>
</jsonFormatter>
</layout>
</encoder>
我想删除根mdc标记以获得:
{"timestamp":"2020-01-20T11:40:09.850Z","level":"INFO","thread":"main","Correlation-ID":"66f7843c-9855-450d-ad97-b1c78404f051","logger":"liquibase.Liquibase"...
我怎样才能做到这一点 - 下面是一个简单的解决方案使用
false
真的 假的 真的 `在这里输入代码`
- 下面是一个简单的解决方案使用
false
真的 假的 真的 `在这里输入代码`
public class CustomJsonLayout extends JsonLayout {
@Override
protected Map toJsonMap(ILoggingEvent iLoggingEvent) {
LinkedHashMap var2 = new LinkedHashMap();
this.addTimestamp("timestamp", this.includeTimestamp, iLoggingEvent.getTimeStamp(), var2);
this.add("level", this.includeLevel, String.valueOf(iLoggingEvent.getLevel()), var2);
this.add("thread", this.includeThreadName, iLoggingEvent.getThreadName(), var2);
this.add("logger", this.includeLoggerName, iLoggingEvent.getLoggerName(), var2);
this.add("message", this.includeFormattedMessage, iLoggingEvent.getFormattedMessage(), var2);
this.add("raw-message", this.includeMessage, iLoggingEvent.getMessage(), var2);
this.add("context", this.includeContextName, iLoggingEvent.getLoggerContextVO().getName(), var2);
this.addThrowableInfo("exception", this.includeException, iLoggingEvent, var2);
this.addCustomDataToJsonMap(var2, iLoggingEvent);
Map<String,String> mdc = iLoggingEvent.getMDCPropertyMap();
String correlationId = mdc.getOrDefault("Correlation-ID","");
this.add("Correlation-ID", this.includeMDC, correlationId , var2);
return var2;
}
}
public类CustomJsonLayout扩展了JsonLayout{
@凌驾
受保护的JSONMAP映射(ILOGINGEVENT ILOGINGEVENT){
LinkedHashMap var2=新LinkedHashMap();
this.addTimestamp(“timestamp”,this.includeTimestamp,ilogingEvent.getTimeStamp(),var2);
add(“level”,this.includeLevel,String.valueOf(ilogingevent.getLevel()),var2);
add(“thread”,this.includeThreadName,ilogingEvent.getThreadName(),var2);
add(“logger”,this.includeLoggerName,ilogingEvent.getLoggerName(),var2);
add(“message”,this.includeFormattedMessage,ilogingEvent.getFormattedMessage(),var2);
add(“原始消息”,this.includeMessage,ilogingEvent.getMessage(),var2);
add(“context”,this.includeContextName,ilogingEvent.getLoggerContextVO().getName(),var2);
this.addThrowableInfo(“exception”,this.includeException,ilogingEvent,var2);
addCustomDataToJsonMap(var2,ilogingEvent);
Map mdc=ilogingevent.getMDCPropertyMap();
String correlationId=mdc.getOrDefault(“相关ID”,”);
添加(“相关ID”,this.includeMDC,correlationId,var2);
返回变量2;
}
}
并在布局中添加新类
<layout class="com.test.utils.CustomJsonLayout">
如果要保留相关Id,可以扩展JSON布局类并在映射上设置属性
public class CustomJsonLayout extends JsonLayout {
@Override
protected Map toJsonMap(ILoggingEvent iLoggingEvent) {
LinkedHashMap var2 = new LinkedHashMap();
this.addTimestamp("timestamp", this.includeTimestamp, iLoggingEvent.getTimeStamp(), var2);
this.add("level", this.includeLevel, String.valueOf(iLoggingEvent.getLevel()), var2);
this.add("thread", this.includeThreadName, iLoggingEvent.getThreadName(), var2);
this.add("logger", this.includeLoggerName, iLoggingEvent.getLoggerName(), var2);
this.add("message", this.includeFormattedMessage, iLoggingEvent.getFormattedMessage(), var2);
this.add("raw-message", this.includeMessage, iLoggingEvent.getMessage(), var2);
this.add("context", this.includeContextName, iLoggingEvent.getLoggerContextVO().getName(), var2);
this.addThrowableInfo("exception", this.includeException, iLoggingEvent, var2);
this.addCustomDataToJsonMap(var2, iLoggingEvent);
Map<String,String> mdc = iLoggingEvent.getMDCPropertyMap();
String correlationId = mdc.getOrDefault("Correlation-ID","");
this.add("Correlation-ID", this.includeMDC, correlationId , var2);
return var2;
}
}
public类CustomJsonLayout扩展了JsonLayout{
@凌驾
受保护的JSONMAP映射(ILOGINGEVENT ILOGINGEVENT){
LinkedHashMap var2=新LinkedHashMap();
this.addTimestamp(“timestamp”,this.includeTimestamp,ilogingEvent.getTimeStamp(),var2);
add(“level”,this.includeLevel,String.valueOf(ilogingevent.getLevel()),var2);
add(“thread”,this.includeThreadName,ilogingEvent.getThreadName(),var2);
add(“logger”,this.includeLoggerName,ilogingEvent.getLoggerName(),var2);
add(“message”,this.includeFormattedMessage,ilogingEvent.getFormattedMessage(),var2);
add(“原始消息”,this.includeMessage,ilogingEvent.getMessage(),var2);
add(“context”,this.includeContextName,ilogingEvent.getLoggerContextVO().getName(),var2);
this.addThrowableInfo(“exception”,this.includeException,ilogingEvent,var2);
addCustomDataToJsonMap(var2,ilogingEvent);
Map mdc=ilogingevent.getMDCPropertyMap();
String correlationId=mdc.getOrDefault(“相关ID”,”);
添加(“相关ID”,this.includeMDC,correlationId,var2);
返回变量2;
}
}
并在布局中添加新类
<layout class="com.test.utils.CustomJsonLayout">