public class MDC
extends java.lang.Object
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.
Modifier and Type | Class and Description |
---|---|
static class |
MDC.MDCCloseable
An adapter to remove the key when done.
|
Modifier and Type | Method and Description |
---|---|
static void |
clear()
Clear all entries in the MDC of the underlying implementation.
|
static java.lang.String |
get(java.lang.String key)
Get the diagnostic context identified by the
key parameter. |
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 MDCAdapter |
getMDCAdapter()
Returns the MDCAdapter instance currently in use.
|
static void |
put(java.lang.String key,
java.lang.String val)
Put a diagnostic context value (the
val parameter) as identified with the
key parameter into the current thread's diagnostic context map. |
static MDC.MDCCloseable |
putCloseable(java.lang.String key,
java.lang.String val)
Put a diagnostic context value (the
val parameter) as identified with the
key parameter into the current thread's diagnostic context map. |
static void |
remove(java.lang.String key)
Remove the diagnostic context identified by the
key parameter using
the underlying system's MDC implementation. |
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.
|
public static void put(java.lang.String key, java.lang.String val) throws java.lang.IllegalArgumentException
val
parameter) as identified with the
key
parameter into the current thread's diagnostic context map. The
key
parameter cannot be null. The val
parameter
can be null only if the underlying implementation supports it.
This method delegates all work to the MDC of the underlying logging system.
key
- non-null keyval
- value to put in the mapjava.lang.IllegalArgumentException
- in case the "key" parameter is nullpublic static MDC.MDCCloseable putCloseable(java.lang.String key, java.lang.String val) throws java.lang.IllegalArgumentException
val
parameter) as identified with the
key
parameter into the current thread's diagnostic context map. The
key
parameter cannot be null. The val
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 remove key
when
close
is called.
Useful with Java 7 for example :
try(MDC.MDCCloseable closeable = MDC.putCloseable(key, value)) {
....
}
key
- non-null keyval
- value to put in the mapCloseable
who can remove key
when close
is called.java.lang.IllegalArgumentException
- in case the "key" parameter is nullpublic static java.lang.String get(java.lang.String key) throws java.lang.IllegalArgumentException
key
parameter. The
key
parameter cannot be null.
This method delegates all work to the MDC of the underlying logging system.
key
- key
parameter.java.lang.IllegalArgumentException
- in case the "key" parameter is nullpublic static void remove(java.lang.String key) throws java.lang.IllegalArgumentException
key
parameter using
the underlying system's MDC implementation. The key
parameter
cannot be null. This method does nothing if there is no previous value
associated with key
.key
- java.lang.IllegalArgumentException
- in case the "key" parameter is nullpublic static void clear()
public static java.util.Map<java.lang.String,java.lang.String> getCopyOfContextMap()
public static void setContextMap(java.util.Map<java.lang.String,java.lang.String> contextMap)
contextMap
- must contain only keys and values of type Stringpublic static MDCAdapter getMDCAdapter()
"Copyright © 2010 - 2020 Adobe Systems Incorporated. All Rights Reserved"