Class MDC
- java.lang.Object
-
- org.slf4j.MDC
-
public class MDC extends java.lang.ObjectThis class hides and serves as a substitute for the underlying logging system's MDC implementation.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,
BasicMDCAdapterwill be used. For other systems, i.e. slf4j-simple and slf4j-nop,NOPMDCAdapterwill 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
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classMDC.MDCCloseableAn adapter to remove the key when done.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static voidclear()Clear all entries in the MDC of the underlying implementation.static java.lang.Stringget(java.lang.String key)Get the diagnostic context identified by thekeyparameter.static java.util.Map<java.lang.String,java.lang.String>getCopyOfContextMap()Return a copy of the current thread's context map, with keys and values of type String.static MDCAdaptergetMDCAdapter()Returns the MDCAdapter instance currently in use.static voidput(java.lang.String key, java.lang.String val)Put a diagnostic context value (thevalparameter) as identified with thekeyparameter into the current thread's diagnostic context map.static MDC.MDCCloseableputCloseable(java.lang.String key, java.lang.String val)Put a diagnostic context value (thevalparameter) as identified with thekeyparameter into the current thread's diagnostic context map.static voidremove(java.lang.String key)Remove the diagnostic context identified by thekeyparameter using the underlying system's MDC implementation.static voidsetContextMap(java.util.Map<java.lang.String,java.lang.String> contextMap)Set the current thread's context map by first clearing any existing map and then copying the map passed as parameter.
-
-
-
Method Detail
-
put
public static void put(java.lang.String key, java.lang.String val) throws java.lang.IllegalArgumentExceptionPut 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 keyval- value to put in the map- Throws:
java.lang.IllegalArgumentException- in case the "key" parameter is null
-
putCloseable
public static MDC.MDCCloseable putCloseable(java.lang.String key, java.lang.String val) throws java.lang.IllegalArgumentException
Put 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 keyval- value to put in the map- Returns:
- a
Closeablewho can removekeywhencloseis called. - Throws:
java.lang.IllegalArgumentException- in case the "key" parameter is null
-
get
public static java.lang.String get(java.lang.String key) throws java.lang.IllegalArgumentExceptionGet 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-- Returns:
- the string value identified by the
keyparameter. - Throws:
java.lang.IllegalArgumentException- in case the "key" parameter is null
-
remove
public static void remove(java.lang.String key) throws java.lang.IllegalArgumentExceptionRemove 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-- Throws:
java.lang.IllegalArgumentException- in case the "key" parameter is null
-
clear
public static void clear()
Clear all entries in the MDC of the underlying implementation.
-
getCopyOfContextMap
public static java.util.Map<java.lang.String,java.lang.String> 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
public static void setContextMap(java.util.Map<java.lang.String,java.lang.String> contextMap)
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
public static MDCAdapter getMDCAdapter()
Returns the MDCAdapter instance currently in use.- Returns:
- the MDcAdapter instance currently in use.
- Since:
- 1.4.2
-
-