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
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic class
An adapter to remove the key when done. -
Field Summary
FieldsModifier and TypeFieldDescription(package private) static MDCAdapter
(package private) static final String
(package private) static final String
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate static MDCAdapter
As of SLF4J version 1.7.14, StaticMDCBinder classes shipping in various bindings come with a getSingleton() method.static void
clear()
Clear all entries in the MDC of the underlying implementation.static String
Get the diagnostic context identified by thekey
parameter.Return a copy of the current thread's context map, with keys and values of type String.static MDCAdapter
Returns the MDCAdapter instance currently in use.static void
Put a diagnostic context value (theval
parameter) as identified with thekey
parameter into the current thread's diagnostic context map.static MDC.MDCCloseable
putCloseable
(String key, String val) Put a diagnostic context value (theval
parameter) as identified with thekey
parameter into the current thread's diagnostic context map.static void
Remove the diagnostic context identified by thekey
parameter using the underlying system's MDC implementation.static void
setContextMap
(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.
-
Field Details
-
NULL_MDCA_URL
- See Also:
-
NO_STATIC_MDC_BINDER_URL
- See Also:
-
mdcAdapter
-
-
Constructor Details
-
MDC
private MDC()
-
-
Method Details
-
bwCompatibleGetMDCAdapterFromBinder
As of SLF4J version 1.7.14, StaticMDCBinder classes shipping in various bindings come with a getSingleton() method. Previously only a public field called SINGLETON was available.- Returns:
- MDCAdapter
- Throws:
NoClassDefFoundError
- in case no binding is available- Since:
- 1.7.14
-
put
Put a diagnostic context value (theval
parameter) as identified with thekey
parameter into the current thread's diagnostic context map. Thekey
parameter cannot be null. Theval
parameter 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:
IllegalArgumentException
- in case the "key" parameter is null
-
putCloseable
Put a diagnostic context value (theval
parameter) as identified with thekey
parameter into the current thread's diagnostic context map. Thekey
parameter cannot be null. Theval
parameter 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
Closeable
object who can removekey
whenclose
is 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
Closeable
who can removekey
whenclose
is called. - Throws:
IllegalArgumentException
- in case the "key" parameter is null
-
get
Get the diagnostic context identified by thekey
parameter. Thekey
parameter 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
key
parameter. - Throws:
IllegalArgumentException
- in case the "key" parameter is null
-
remove
Remove the diagnostic context identified by thekey
parameter using the underlying system's MDC implementation. Thekey
parameter cannot be null. This method does nothing if there is no previous value associated withkey
.- Parameters:
key
-- 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
-