Getting Started with MDC


1. What is MDC?
Let’s start with an original comment
quote
/**
* 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
*/ 


This is a comment on the MDC class, but it doesn't say what this is for. The specific content is on a page:
Chapter 8: Mapped Diagnostic Context
is simply an enhancement of the log. If MDC is configured and the corresponding key value is added, the value corresponding to the key will be printed out when logging. .
Internally, it is implemented with ThreadLocal, which can carry the context information of the current thread.

2. MDCAdapter - LogbackMDCAdapter
MDC wraps an MDCAdapter, which is an interface, and different log packages have their own corresponding implementations. For packages that do not support MDC, such as java.util.logging, use the default BasicMDCAdapter, slf4j-nop has NOPMDCAdapter.
The implementation mentioned here is LogbackMDCAdapter.

Reference:
http://blog.csdn.net/xxcupid/article/details/51955356

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326739265&siteId=291194637