Class ThreadContext
The MDC is managed on a per thread basis. To enable automatic inheritance of copies of the MDC to newly created threads, enable the "isThreadContextMapInheritable" Log4j system property.
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceThe ThreadContext Stack interface. -
Field Summary
FieldsModifier and TypeFieldDescriptionEmpty, immutable Map.static final ThreadContextStackEmpty, immutable ContextStack. -
Method Summary
Modifier and TypeMethodDescriptionstatic voidclearAll()Clears the context map and stack.static voidclearMap()Clears the context map.static voidClears the stack for this thread.static ThreadContext.ContextStackReturns a copy of this thread's stack.static booleancontainsKey(String key) Determines if the key is in the context.static StringGets the context value identified by thekeyparameter.Returns a mutable copy of current thread's context Map.static intgetDepth()Gets the current nesting depth of this thread's stack.Returns an immutable view of the current thread's context Map.static ThreadContext.ContextStackGets an immutable copy of this current thread's context stack.static ReadOnlyThreadContextMapReturns a read-only view of the internal data structure used to store thread context key-value pairs, ornullif the internal data structure does not implement theReadOnlyThreadContextMapinterface.static voidinit()Consider private, used for testing.static booleanisEmpty()Returns true if the Map is empty.static Stringpeek()Looks at the last diagnostic context at the top of this NDC without removing it.static Stringpop()Returns the value of the last item placed on the stack.static voidPushes new diagnostic context information for the current thread.static voidPushes new diagnostic context information for the current thread.static voidPuts a context value (thevalueparameter) as identified with thekeyparameter into the current thread's context map.static voidPuts all given context map entries into the current thread's context map.static voidPuts a context value (thevalueparameter) as identified with thekeyparameter into the current thread's context map if the key does not exist.static voidRemoves the context value identified by thekeyparameter.static voidRemoves the context values identified by thekeysparameter.static voidRemoves the diagnostic context for this thread.static voidsetStack(Collection<String> stack) Sets this thread's stack.static voidtrim(int depth) Trims elements from this diagnostic context.
-
Field Details
-
EMPTY_MAP
Empty, immutable Map. -
EMPTY_STACK
Empty, immutable ContextStack.
-
-
Method Details
-
init
public static void init()Consider private, used for testing.- Since:
- 2.20.0
-
put
Puts a context value (thevalueparameter) as identified with thekeyparameter into the current thread's context map.If the current thread does not have a context map it is created as a side effect.
- Parameters:
key- The key name.value- The key value.
-
putIfNull
Puts a context value (thevalueparameter) as identified with thekeyparameter into the current thread's context map if the key does not exist.If the current thread does not have a context map it is created as a side effect.
- Parameters:
key- The key name.value- The key value.- Since:
- 2.13.0
-
putAll
Puts all given context map entries into the current thread's context map.If the current thread does not have a context map it is created as a side effect.
- Parameters:
m- The map.- Since:
- 2.7
-
get
Gets the context value identified by thekeyparameter.This method has no side effects.
- Parameters:
key- The key to locate.- Returns:
- The value associated with the key or null.
-
remove
Removes the context value identified by thekeyparameter.- Parameters:
key- The key to remove.
-
removeAll
Removes the context values identified by thekeysparameter.- Parameters:
keys- The keys to remove.- Since:
- 2.8
-
clearMap
public static void clearMap()Clears the context map. -
clearAll
public static void clearAll()Clears the context map and stack. -
containsKey
Determines if the key is in the context.- Parameters:
key- The key to locate.- Returns:
- True if the key is in the context, false otherwise.
-
getContext
Returns a mutable copy of current thread's context Map.- Returns:
- a mutable copy of the context.
-
getImmutableContext
Returns an immutable view of the current thread's context Map.- Returns:
- An immutable view of the ThreadContext Map.
-
getThreadContextMap
Returns a read-only view of the internal data structure used to store thread context key-value pairs, ornullif the internal data structure does not implement theReadOnlyThreadContextMapinterface.The
DefaultThreadContextMapimplementation does not implementReadOnlyThreadContextMap, so by default this method returnsnull.- Returns:
- the internal data structure used to store thread context key-value pairs or
null - Since:
- 2.8
- See Also:
-
isEmpty
public static boolean isEmpty()Returns true if the Map is empty.- Returns:
- true if the Map is empty, false otherwise.
-
clearStack
public static void clearStack()Clears the stack for this thread. -
cloneStack
Returns a copy of this thread's stack.- Returns:
- A copy of this thread's stack.
-
getImmutableStack
Gets an immutable copy of this current thread's context stack.- Returns:
- an immutable copy of the ThreadContext stack.
-
setStack
Sets this thread's stack.- Parameters:
stack- The stack to use.
-
getDepth
public static int getDepth()Gets the current nesting depth of this thread's stack.- Returns:
- the number of items in the stack.
- See Also:
-
pop
Returns the value of the last item placed on the stack.The returned value is the value that was pushed last. If no context is available, then the empty string "" is returned.
- Returns:
- String The innermost diagnostic context.
-
peek
Looks at the last diagnostic context at the top of this NDC without removing it.The returned value is the value that was pushed last. If no context is available, then the empty string "" is returned.
- Returns:
- String The innermost diagnostic context.
-
push
Pushes new diagnostic context information for the current thread.The contents of the
messageparameter is determined solely by the client.- Parameters:
message- The new diagnostic context information.
-
push
Pushes new diagnostic context information for the current thread.The contents of the
messageand args parameters are determined solely by the client. The message will be treated as a format String and tokens will be replaced with the String value of the arguments in accordance with ParameterizedMessage.- Parameters:
message- The new diagnostic context information.args- Parameters for the message.
-
removeStack
public static void removeStack()Removes the diagnostic context for this thread.Each thread that created a diagnostic context by calling
push(java.lang.String)should call this method before exiting. Otherwise, the memory used by the thread cannot be reclaimed by the VM.As this is such an important problem in heavy duty systems and because it is difficult to always guarantee that the remove method is called before exiting a thread, this method has been augmented to lazily remove references to dead threads. In practice, this means that you can be a little sloppy and occasionally forget to call
remove(java.lang.String)before exiting a thread. However, you must callremovesometime. If you never call it, then your application is sure to run out of memory. -
trim
public static void trim(int depth) Trims elements from this diagnostic context. If the current depth is smaller or equal tomaxDepth, then no action is taken. If the current depth is larger than newDepth then all elements at maxDepth or higher are discarded.This method is a convenient alternative to multiple
pop()calls. Moreover, it is often the case that at the end of complex call sequences, the depth of the ThreadContext is unpredictable. Thetrimmethod circumvents this problem.For example, the combination
void foo() { final int depth = ThreadContext.getDepth(); // ... complex sequence of calls ThreadContext.trim(depth); }ensures that between the entry and exit of
foothe depth of the diagnostic stack is conserved.- Parameters:
depth- The number of elements to keep.- See Also:
-