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.
| Modifier and Type | Class and Description |
|---|---|
| public static interface | ThreadContext.
The ThreadContext Stack interface. |
| private static class | |
| private static class | ThreadContext.
An empty read-only ThreadContextStack. |
| Modifier and Type | Field and Description |
|---|---|
| private static ThreadContextMap | |
| private static ThreadContextStack | |
| private static final String | |
| private static final String | |
| private static final String | |
| public static final Map | EMPTY_MAP
Empty, immutable Map. |
| public static final ThreadContextStack | EMPTY_STACK
Empty, immutable ContextStack. |
| private static ReadOnlyThreadContextMap | |
| private static boolean |
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| public static void | |
| public static void | |
| public static void | |
| public static ThreadContext. | |
| public static boolean | Returns: True if the key is in the context, false otherwise.The key to locate. key)Determines if the key is in the context. |
| public static String | |
| public static Map | Returns: a mutable copy of the context.Returns a mutable copy of current thread's context Map. |
| public static int | Returns: the number of items in the stack.Gets the current nesting depth of this thread's stack. |
| public static Map | Returns: An immutable view of the ThreadContext Map.Returns an immutable view of the current thread's context Map. |
| public static ThreadContext. | Returns: an immutable copy of the ThreadContext stack.Gets an immutable copy of this current thread's context stack. |
| public static ReadOnlyThreadContextMap | Returns: the internal data structure used to store thread context key-value pairs ornullReturns a read-only view of the internal data structure used to store thread context key-value pairs,
or |
| public static void | |
| public static boolean | |
| public static String | Returns: String The innermost diagnostic context.Looks at the last diagnostic context at the top of this NDC without removing it. |
| public static String | Returns: String The innermost diagnostic context.Returns the value of the last item placed on the stack. |
| public static void | |
| public static void | |
| public static void | |
| public static void | |
| public static void | |
| public static void | |
| public static void | |
| public static void | |
| public static void | |
| public static void |
| contextMap | back to summary |
|---|---|
| private static ThreadContextMap contextMap | |
| contextStack | back to summary |
|---|---|
| private static ThreadContextStack contextStack | |
| DISABLE_ALL | back to summary |
|---|---|
| private static final String DISABLE_ALL | |
| DISABLE_MAP | back to summary |
|---|---|
| private static final String DISABLE_MAP | |
| DISABLE_STACK | back to summary |
|---|---|
| private static final String DISABLE_STACK | |
| EMPTY_MAP | back to summary |
|---|---|
| public static final Map<String, String> EMPTY_MAP Empty, immutable Map.
| |
| EMPTY_STACK | back to summary |
|---|---|
| public static final ThreadContextStack EMPTY_STACK Empty, immutable ContextStack.
| |
| readOnlyContextMap | back to summary |
|---|---|
| private static ReadOnlyThreadContextMap readOnlyContextMap | |
| useStack | back to summary |
|---|---|
| private static boolean useStack | |
| ThreadContext | back to summary |
|---|---|
| private ThreadContext() | |
| clearAll | back to summary |
|---|---|
| public static void clearAll() Clears the context map and stack. | |
| clearMap | back to summary |
|---|---|
| public static void clearMap() Clears the context map. | |
| clearStack | back to summary |
|---|---|
| public static void clearStack() Clears the stack for this thread. | |
| cloneStack | back to summary |
|---|---|
| public static ThreadContext. Returns a copy of this thread's stack.
| |
| containsKey | back to summary |
|---|---|
| public static boolean containsKey(final String key) Determines if the key is in the context.
| |
| get | back to summary |
|---|---|
| public static String get(final String key) Gets the context value identified by the This method has no side effects. | |
| getContext | back to summary |
|---|---|
| public static Map Returns a mutable copy of current thread's context Map. | |
| getDepth | back to summary |
|---|---|
| public static int getDepth() Gets the current nesting depth of this thread's stack.
| |
| getImmutableContext | back to summary |
|---|---|
| public static Map Returns an immutable view of the current thread's context Map. | |
| getImmutableStack | back to summary |
|---|---|
| public static ThreadContext. Gets an immutable copy of this current thread's context stack.
| |
| getThreadContextMap | back to summary |
|---|---|
| public static ReadOnlyThreadContextMap getThreadContextMap() Returns a read-only view of the internal data structure used to store thread context key-value pairs,
or
The
| |
| init | back to summary |
|---|---|
| public static void init() Consider private, used for testing. | |
| isEmpty | back to summary |
|---|---|
| public static boolean isEmpty() Returns true if the Map is empty.
| |
| peek | back to summary |
|---|---|
| public static String 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.
| |
| pop | back to summary |
|---|---|
| public static String 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.
| |
| push | back to summary |
|---|---|
| public static void push(final String message) Pushes new diagnostic context information for the current thread.
The contents of the
| |
| push | back to summary |
|---|---|
| public static void push(final String message, final Object... args) Pushes new diagnostic context information for the current thread.
The contents of the | |
| put | back to summary |
|---|---|
| public static void put(final String key, final String value) Puts a context value (the If the current thread does not have a context map it is created as a side effect. | |
| putAll | back to summary |
|---|---|
| public static void putAll(final Map<String, String> m) 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. | |
| putIfNull | back to summary |
|---|---|
| public static void putIfNull(final String key, final String value) Puts a context value (the If the current thread does not have a context map it is created as a side effect. | |
| remove | back to summary |
|---|---|
| public static void remove(final String key) Removes the context value identified by the
| |
| removeAll | back to summary |
|---|---|
| public static void removeAll(final Iterable<String> keys) Removes the context values identified by the | |
| removeStack | back to summary |
|---|---|
| public static void removeStack() Removes the diagnostic context for this thread.
Each thread that created a diagnostic context by calling
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
| |
| setStack | back to summary |
|---|---|
| public static void setStack(final Collection<String> stack) Sets this thread's stack.
| |
| trim | back to summary |
|---|---|
| public static void trim(final int depth) Trims elements from this diagnostic context. If the current depth is smaller or equal to
This method is a convenient alternative to multiple 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
| |
| Modifier and Type | Method and Description |
|---|---|
| public List | Returns: all the elements in the stack in a List.Returns all the elements in the stack in a List. |
| public ThreadContext. | |
| public int | |
| public ThreadContext. | Returns: a ContextStack with the same contents as this ContextStack ornull.Returns a ContextStack with the same contents as this ContextStack or |
| public String | Returns: the element at the top of the stack or null if the stack is empty.Returns the element at the top of the stack without removing it or null if the stack is empty. |
| public String | |
| public void | |
| public void | trim(int
The maximum number of items in the stack to keep. depth)Trims elements from the end of the stack. |
| asList | back to summary |
|---|---|
| public List Returns all the elements in the stack in a List. | |
| copy | back to summary |
|---|---|
| public ThreadContext. Returns a copy of the ContextStack.
| |
| getDepth | back to summary |
|---|---|
| public int getDepth() Returns the number of elements in the stack.
| |
| getImmutableStackOrNull | back to summary |
|---|---|
| public ThreadContext. Returns a ContextStack with the same contents as this ContextStack or
| |
| peek | back to summary |
|---|---|
| public String peek() Returns the element at the top of the stack without removing it or null if the stack is empty.
| |
| pop | back to summary |
|---|---|
| public String pop() Returns the element at the top of the stack.
| |
| push | back to summary |
|---|---|
| public void push(String message) Pushes an element onto the stack.
| |
| trim | back to summary |
|---|---|
| public void trim(int depth) Trims elements from the end of the stack.
| |
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| public boolean | |
| public E | |
| public void | remove()
Overrides default java. Removes from the underlying collection the last element returned by this iterator (optional operation). |
| EmptyIterator | back to summary |
|---|---|
| private EmptyIterator() | |
| hasNext | back to summary |
|---|---|
| public boolean hasNext() Implements java. Doc from java. Returns
| |
| next | back to summary |
|---|---|
| public E next() Implements java. Doc from java. Returns the next element in the iteration.
| |
| remove | back to summary |
|---|---|
| public void remove() Overrides default java. Doc from java. Removes from the underlying collection the last element returned
by this iterator (optional operation). This method can be called
only once per call to The behavior of an iterator is unspecified if the underlying collection is modified while the iteration is in progress in any way other than by calling this method, unless an overriding class has specified a concurrent modification policy.
The behavior of an iterator is unspecified if this method is called
after a call to the
| |
| Modifier and Type | Field and Description |
|---|---|
| private static final Iterator | |
| private static final long |
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| public boolean | add(final String
element whose presence in this collection is to be ensured e)Overrides java. Implements java. Ensures that this collection contains the specified element (optional operation). |
| public boolean | addAll(final Collection<? extends String>
collection containing elements to be added to this collection c)Overrides java. Implements java. Adds all of the elements in the specified collection to this collection (optional operation). |
| public List | asList()
Implements org. Returns all the elements in the stack in a List. |
| public boolean | containsAll(final Collection<?>
collection to be checked for containment in this collection c)Overrides java. Implements java. Returns |
| public ThreadContext. | copy()
Implements org. Returns a copy of the ContextStack. |
| public boolean | equals(final Object
object to be compared for equality with this collection o)Overrides java. Implements java. Compares the specified object with this collection for equality. |
| public int | getDepth()
Implements org. Returns the number of elements in the stack. |
| public ThreadContext. | getImmutableStackOrNull()
Implements org. Returns a ContextStack with the same contents as this ContextStack or |
| public int | hashCode()
Overrides java. Implements java. Returns the hash code value for this collection. |
| public Iterator | iterator()
Implements abstract java. Implements java. Returns an iterator over the elements in this collection. |
| public String | peek()
Implements org. Returns the element at the top of the stack without removing it or null if the stack is empty. |
| public String | pop()
Implements org. Returns the element at the top of the stack. |
| public void | push(final String
The element to add. message)Implements org. Pushes an element onto the stack. |
| public boolean | removeAll(final Collection<?>
collection containing elements to be removed from this collection c)Overrides java. Implements java. Removes all of this collection's elements that are also contained in the specified collection (optional operation). |
| public boolean | retainAll(final Collection<?>
collection containing elements to be retained in this collection c)Overrides java. Implements java. Retains only the elements in this collection that are contained in the specified collection (optional operation). |
| public int | size()
Implements abstract java. Implements java. Returns the number of elements in this collection. |
| public <T> T[] | toArray(final T[]
the array into which the elements of this collection are to be
stored, if it is big enough; otherwise, a new array of the same
runtime type is allocated for this purpose. a)Overrides java. Implements java. Returns an array containing all of the elements in this collection; the runtime type of the returned array is that of the specified array. |
| public void | trim(final int
The maximum number of items in the stack to keep. depth)Implements org. Trims elements from the end of the stack. |
| EMPTY_ITERATOR | back to summary |
|---|---|
| private static final Iterator<String> EMPTY_ITERATOR | |
| serialVersionUID | back to summary |
|---|---|
| private static final long serialVersionUID | |
| EmptyThreadContextStack | back to summary |
|---|---|
| private EmptyThreadContextStack() | |
| add | back to summary |
|---|---|
| public boolean add(final String e) Overrides java. Implements java. Doc from java. Ensures that this collection contains the specified element (optional
operation). Returns
Collections that support this operation may place limitations on what
elements may be added to this collection. In particular, some
collections will refuse to add
If a collection refuses to add a particular element for any reason
other than that it already contains the element, it must throw
an exception (rather than returning | |
| addAll | back to summary |
|---|---|
| public boolean addAll(final Collection<? extends String> c) Overrides java. Implements java. Doc from java. Adds all of the elements in the specified collection to this collection (optional operation). The behavior of this operation is undefined if the specified collection is modified while the operation is in progress. (This implies that the behavior of this call is undefined if the specified collection is this collection, and this collection is nonempty.) If the specified collection has a defined encounter order, processing of its elements generally occurs in that order.
| |
| asList | back to summary |
|---|---|
| public List Implements org. Doc from org. Returns all the elements in the stack in a List. | |
| containsAll | back to summary |
|---|---|
| public boolean containsAll(final Collection<?> c) Overrides java. Implements java. Doc from java. Returns
| |
| copy | back to summary |
|---|---|
| public ThreadContext. Implements org. Doc from org. Returns a copy of the ContextStack.
| |
| equals | back to summary |
|---|---|
| public boolean equals(final Object o) Overrides java. Implements java. Doc from java. Compares the specified object with this collection for equality.
While the
The general contract for the | |
| getDepth | back to summary |
|---|---|
| public int getDepth() Implements org. Doc from org. Returns the number of elements in the stack.
| |
| getImmutableStackOrNull | back to summary |
|---|---|
| public ThreadContext. Implements org. Doc from org. Returns a ContextStack with the same contents as this ContextStack or
| |
| hashCode | back to summary |
|---|---|
| public int hashCode() Overrides java. Implements java. Doc from java. Returns the hash code value for this collection. While the
| |
| iterator | back to summary |
|---|---|
| public Iterator Implements abstract java. Implements java. Doc from java. Returns an iterator over the elements in this collection. There are no guarantees concerning the order in which the elements are returned (unless this collection is an instance of some class that provides a guarantee). | |
| peek | back to summary |
|---|---|
| public String peek() Implements org. Doc from org. Returns the element at the top of the stack without removing it or null if the stack is empty. | |
| pop | back to summary |
|---|---|
| public String pop() Implements org. Doc from org. Returns the element at the top of the stack. | |
| push | back to summary |
|---|---|
| public void push(final String message) Implements org. Doc from org. Pushes an element onto the stack. | |
| removeAll | back to summary |
|---|---|
| public boolean removeAll(final Collection<?> c) Overrides java. Implements java. Doc from java. Removes all of this collection's elements that are also contained in the specified collection (optional operation). After this call returns, this collection will contain no elements in common with the specified collection.
| |
| retainAll | back to summary |
|---|---|
| public boolean retainAll(final Collection<?> c) Overrides java. Implements java. Doc from java. Retains only the elements in this collection that are contained in the specified collection (optional operation). In other words, removes from this collection all of its elements that are not contained in the specified collection.
| |
| size | back to summary |
|---|---|
| public int size() Implements abstract java. Implements java. Doc from java. Returns the number of elements in this collection. If this collection
contains more than
| |
| toArray | back to summary |
|---|---|
| public <T> T[] toArray(final T[] a) Overrides java. Implements java. Doc from java. Returns an array containing all of the elements in this collection; the runtime type of the returned array is that of the specified array. If the collection fits in the specified array, it is returned therein. Otherwise, a new array is allocated with the runtime type of the specified array and the size of this collection. If this collection fits in the specified array with room to spare
(i.e., the array has more elements than this collection), the element
in the array immediately following the end of the collection is set to
If this collection makes any guarantees as to what order its elements are returned by its iterator, this method must return the elements in the same order.
| |
| trim | back to summary |
|---|---|
| public void trim(final int depth) Implements org. Doc from org. Trims elements from the end of the stack.
| |