Class MDC
 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,
 BasicMDCAdapter will be used. For other systems, i.e. slf4j-simple
 and slf4j-nop, NOPMDCAdapter will be used.
 
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.
For more information on MDC please see the chapter on MDC in the logback manual.
Please note that all methods in this class are static.
- Since:
- 1.4.1
- Author:
- Ceki Gülcü
- 
Nested Class SummaryNested ClassesModifier and TypeClassDescriptionstatic classAn adapter to remove the key when done.
- 
Method SummaryModifier and TypeMethodDescriptionstatic voidclear()Clear all entries in the MDC of the underlying implementation.static StringGet the diagnostic context identified by thekeyparameter.Return a copy of the current thread's context map, with keys and values of type String.Returns a copy of the deque(stack) referenced by 'key'.static MDCAdapterReturns the MDCAdapter instance currently in use.static StringPop the stack referenced by 'key' and return the value possibly null.static voidPush a value into the deque(stack) referenced by 'key'.static voidPut a diagnostic context value (thevalparameter) as identified with thekeyparameter into the current thread's diagnostic context map.static MDC.MDCCloseableputCloseable(String key, String val) Put a diagnostic context value (thevalparameter) as identified with thekeyparameter into the current thread's diagnostic context map.static voidRemove the diagnostic context identified by thekeyparameter using the underlying system's MDC implementation.static voidsetContextMap(Map<String, String> contextMap) Set the current thread's context map by first clearing any existing map and then copying the map passed as parameter.
- 
Method Details- 
putPut a diagnostic context value (thevalparameter) as identified with thekeyparameter into the current thread's diagnostic context map. Thekeyparameter cannot be null. Thevalparameter can be null only if the underlying implementation supports it.This method delegates all work to the MDC of the underlying logging system. - Parameters:
- key- non-null key
- val- value to put in the map
- Throws:
- IllegalArgumentException- in case the "key" parameter is null
 
- 
putCloseablePut a diagnostic context value (thevalparameter) as identified with thekeyparameter into the current thread's diagnostic context map. Thekeyparameter cannot be null. Thevalparameter can be null only if the underlying implementation supports it.This method delegates all work to the MDC of the underlying logging system. This method return a Closeableobject who can removekeywhencloseis called.Useful with Java 7 for example : try(MDC.MDCCloseable closeable = MDC.putCloseable(key, value)) { .... }- Parameters:
- key- non-null key
- val- value to put in the map
- Returns:
- a Closeablewho can removekeywhencloseis called.
- Throws:
- IllegalArgumentException- in case the "key" parameter is null
 
- 
getGet the diagnostic context identified by thekeyparameter. Thekeyparameter cannot be null.This method delegates all work to the MDC of the underlying logging system. - Parameters:
- key- a key
- Returns:
- the string value identified by the keyparameter.
- Throws:
- IllegalArgumentException- in case the "key" parameter is null
 
- 
removeRemove the diagnostic context identified by thekeyparameter using the underlying system's MDC implementation. Thekeyparameter cannot be null. This method does nothing if there is no previous value associated withkey.- Parameters:
- key- a key
- Throws:
- IllegalArgumentException- in case the "key" parameter is null
 
- 
clearClear all entries in the MDC of the underlying implementation.
- 
getCopyOfContextMapReturn a copy of the current thread's context map, with keys and values of type String. Returned value may be null.- Returns:
- A copy of the current thread's context map. May be null.
- Since:
- 1.5.1
 
- 
setContextMapSet the current thread's context map by first clearing any existing map and then copying the map passed as parameter. The context map passed as parameter must only contain keys and values of type String. Null valued argument is allowed (since SLF4J version 2.0.0).- Parameters:
- contextMap- must contain only keys and values of type String
- Since:
- 1.5.1
 
- 
getMDCAdapterReturns the MDCAdapter instance currently in use. Since 2.0.17, if the MDCAdapter instance is null, then this method set it to use the adapter returned by the SLF4JProvider. However, in the vast majority of cases the MDCAdapter will be set earlier (during initialization) byLoggerFactory.- Returns:
- the MDcAdapter instance currently in use.
- Since:
- 1.4.2
 
- 
pushByKey
- 
popByKey
- 
getCopyOfDequeByKeyReturns a copy of the deque(stack) referenced by 'key'. May be null.- Parameters:
- key- identifies the stack
- Returns:
- copy of stack referenced by 'key'. May be null.
- Since:
- 2.0.0
 
 
-