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. If the underlying system does not support MDC, e.g. java.util.logging,
then SLF4J will use a BasicMDCAdapter.
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
-
Method Summary
Modifier and TypeMethodDescriptionstatic voidclear()Clear all entries in the MDC of the underlying implementation.static StringGet the context identified by thekeyparameter.static MapReturn a copy of the current thread's context map, with keys and values of type String.static MDCAdapterReturns the MDCAdapter instance currently in use.static voidPut a context value (thevalparameter) as identified with thekeyparameter into the current thread's context map.static voidRemove the the context identified by thekeyparameter using the underlying system's MDC implementation.static voidsetContextMap(Map contextMap) Set the current thread's context map by first clearing any existing map and then copying the map passed as parameter.
-
Method Details
-
put
Put a context value (thevalparameter) as identified with thekeyparameter into the current thread's 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.
- Throws:
IllegalArgumentException- in case the "key" parameter is null
-
get
Get the context identified by thekeyparameter. Thekeyparameter cannot be null.This method delegates all work to the MDC of the underlying logging system.
- Returns:
- the string value identified by the
keyparameter. - Throws:
IllegalArgumentException- in case the "key" parameter is null
-
remove
Remove the the 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.- Throws:
IllegalArgumentException- in case the "key" parameter is null
-
clear
public static void clear()Clear all entries in the MDC of the underlying implementation. -
getCopyOfContextMap
Return 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
-
setContextMap
Set 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.- Parameters:
contextMap- must contain only keys and values of type String- Since:
- 1.5.1
-
getMDCAdapter
Returns the MDCAdapter instance currently in use.- Returns:
- the MDcAdapter instance currently in use.
- Since:
- 1.4.2
-