【问题标题】:Slf4j, logback - removing mdc tag from jsonSlf4j,logback - 从 json 中删除 mdc 标签
【发布时间】:2020-01-20 12:01:27
【问题描述】:

我正在使用 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.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> 

我有以下日志:

{"timestamp":"2020-01-20T11:40:09.850Z","level":"INFO","thread":"main","mdc":{"Correlation-ID":"66f7843c-9855-450d-ad97-b1c78404f051"},"logger":"liquibase.Liquibase"...

我想删除根 mdc 标记以获取:

{"timestamp":"2020-01-20T11:40:09.850Z","level":"INFO","thread":"main","Correlation-ID":"66f7843c-9855-450d-ad97-b1c78404f051","logger":"liquibase.Liquibase"...

我将如何实现这一目标?

【问题讨论】:

    标签: java logback mdc


    【解决方案1】:
    • 这是一个简单的解决方案,使用&lt;includeMDC&gt;false&lt;/includeMDC&gt;

      <layout class="ch.qos.logback.contrib.json.classic.JsonLayout">
                     <appendLineSeparator>true</appendLineSeparator>
                    <includeMDC>false</includeMDC>
                  <jsonFormatter
                      class="ch.qos.logback.contrib.jackson.JacksonJsonFormatter">
                      <prettyPrint>true</prettyPrint>
                  </jsonFormatter>`enter code here`
              </layout>
      

    【讨论】:

      【解决方案2】:

      如果您想保留关联 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;
      }
      }
      

      并在布局中添加新类。

       <layout class="com.test.utils.CustomJsonLayout">
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-14
        • 2016-04-15
        相关资源
        最近更新 更多