1.MDC是什么
先来一段原版注释,
/**
* This class hides and serves as a substitute for the underlying logging
* system's MDC implementation.
*
* <p>
* If the underlying logging system offers MDC functionality, then SLF4J's MDC,
* i.e. this class, will delegate to the underlying system's MDC. Note that at
* this time, only two logging systems, namely log4j and logback, offer MDC
* functionality. For java.util.logging which does not support MDC,
* {@link BasicMDCAdapter} will be used. For other systems, i.e slf4j-simple
* and slf4j-nop, {@link NOPMDCAdapter} will be used.
*
* <p>
* Thus, as a SLF4J user, you can take advantage of MDC in the presence of log4j,
* logback, or java.util.logging, but without forcing these systems as
* dependencies upon your users.
*
* <p>
* For more information on MDC please see the <a
* href="http://logback.qos.ch/manual/mdc.html">chapter on MDC</a> in the
* logback manual.
*
* <p>
* Please note that all methods in this class are static.
*
* @author Ceki Gülcü
* @since 1.4.1
*/
这是MDC类上的注释,但是没有讲这个是干什么的。具体的内容在有一个页面:
Chapter 8: Mapped Diagnostic Context
简单来说就是日志的增强功能,如果配置了MDC,并添加了相应的key value,就会在打日志的时候把key对应的value打印出来。
内部是用ThreadLocal来实现的,可以携带当前线程的context信息。
2.MDCAdapter——LogbackMDCAdapter
MDC中包装了一个MDCAdapter,这是一个接口,不同的log包有自己对应的实现。对于不支持MDC的包,比如java.util.logging,使用默认的BasicMDCAdapter,slf4j-nop有NOPMDCAdapter。
这里说的实现是LogbackMDCAdapter。