Top Description Inners Constructors Methods
org.apache.logging.log4j

public Class CloseableThreadContext

extends Object
Class Inheritance
Imports
java.util.HashMap, .Iterator, .List, .Map

Adds entries to the stack or map and them removes them when the object is closed, e.g. as part of a try-with-resources. User code can now look like this:
try (final CloseableThreadContext.Instance ignored = CloseableThreadContext.put(key1, value1).put(key2, value2)) {
    callSomeMethodThatLogsALot();

// Entries for key1 and key2 are automatically removed from the ThreadContext map when done.
}
Since
2.6

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static class

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static CloseableThreadContext.Instance

Returns:

a new instance that will back out the changes when closed.
push
(final String
The new diagnostic context information.
message
)

Pushes new diagnostic context information on to the Thread Context Stack.

public static CloseableThreadContext.Instance

Returns:

a new instance that will back out the changes when closed.
push
(final String
The new diagnostic context information.
message
,
final Object...
Parameters for the message.
args
)

Pushes new diagnostic context information on to the Thread Context Stack.

public static CloseableThreadContext.Instance

Returns:

a new instance that will back out the changes when closed.
pushAll
(final List<String>
The list of messages to be added
messages
)

Populates the Thread Context Stack with the supplied stack.

public static CloseableThreadContext.Instance

Returns:

a new instance that will back out the changes when closed.
put
(final String
The key to be added
key
,
final String
The value to be added
value
)

Populates the Thread Context Map with the supplied key/value pair.

public static CloseableThreadContext.Instance

Returns:

a new instance that will back out the changes when closed.
putAll
(final Map<String, String>
The map of key/value pairs to be added
values
)

Populates the Thread Context Map with the supplied key/value pairs.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

CloseableThreadContextback to summary
private CloseableThreadContext()

Method Detail

pushback to summary
public static CloseableThreadContext.Instance push(final String message)

Pushes new diagnostic context information on to the Thread Context Stack. The information will be popped off when the instance is closed.

Parameters
message:String

The new diagnostic context information.

Returns:CloseableThreadContext.Instance

a new instance that will back out the changes when closed.

pushback to summary
public static CloseableThreadContext.Instance push(final String message, final Object... args)

Pushes new diagnostic context information on to the Thread Context Stack. The information will be popped off when the instance is closed.

Parameters
message:String

The new diagnostic context information.

args:Object[]

Parameters for the message.

Returns:CloseableThreadContext.Instance

a new instance that will back out the changes when closed.

pushAllback to summary
public static CloseableThreadContext.Instance pushAll(final List<String> messages)

Populates the Thread Context Stack with the supplied stack. The information will be popped off when the instance is closed.

Parameters
messages:List<String>

The list of messages to be added

Returns:CloseableThreadContext.Instance

a new instance that will back out the changes when closed.

Since
2.8
putback to summary
public static CloseableThreadContext.Instance put(final String key, final String value)

Populates the Thread Context Map with the supplied key/value pair. Any existing key in the ThreadContext will be replaced with the supplied value, and restored back to their original value when the instance is closed.

Parameters
key:String

The key to be added

value:String

The value to be added

Returns:CloseableThreadContext.Instance

a new instance that will back out the changes when closed.

putAllback to summary
public static CloseableThreadContext.Instance putAll(final Map<String, String> values)

Populates the Thread Context Map with the supplied key/value pairs. Any existing keys in the ThreadContext will be replaced with the supplied values, and restored back to their original value when the instance is closed.

Parameters
values:Map<String, String>

The map of key/value pairs to be added

Returns:CloseableThreadContext.Instance

a new instance that will back out the changes when closed.

Since
2.8
org.apache.logging.log4j back to summary

public Class CloseableThreadContext.Instance

extends Object
implements AutoCloseable
Class Inheritance
All Implemented Interfaces
java.lang.AutoCloseable

Field Summary

Modifier and TypeField and Description
private final Map<String, String>
private int

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public void
close()

Implements java.lang.AutoCloseable.close.

Removes the values from the ThreadContext.

private void
private void
public CloseableThreadContext.Instance

Returns:

the instance that will back out the changes when closed.
push
(final String
The new diagnostic context information.
message
)

Pushes new diagnostic context information on to the Thread Context Stack.

public CloseableThreadContext.Instance

Returns:

the instance that will back out the changes when closed.
push
(final String
The new diagnostic context information.
message
,
final Object[]
Parameters for the message.
args
)

Pushes new diagnostic context information on to the Thread Context Stack.

public CloseableThreadContext.Instance

Returns:

a new instance that will back out the changes when closed.
pushAll
(final List<String>
The list of messages to be added
messages
)

Populates the Thread Context Stack with the supplied stack.

public CloseableThreadContext.Instance

Returns:

a new instance that will back out the changes when closed.
put
(final String
The key to be added
key
,
final String
The value to be added
value
)

Populates the Thread Context Map with the supplied key/value pair.

public CloseableThreadContext.Instance

Returns:

a new instance that will back out the changes when closed.
putAll
(final Map<String, String>
The map of key/value pairs to be added
values
)

Populates the Thread Context Map with the supplied key/value pairs.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

originalValuesback to summary
private final Map<String, String> originalValues
pushCountback to summary
private int pushCount

Constructor Detail

Instanceback to summary
private Instance()

Method Detail

closeback to summary
public void close()

Implements java.lang.AutoCloseable.close.

Removes the values from the ThreadContext.

Values pushed to the ThreadContext stack will be popped off.

Values put on the ThreadContext map will be removed, or restored to their original values it they already existed.

Annotations
@Override
closeMapback to summary
private void closeMap()
closeStackback to summary
private void closeStack()
pushback to summary
public CloseableThreadContext.Instance push(final String message)

Pushes new diagnostic context information on to the Thread Context Stack. The information will be popped off when the instance is closed.

Parameters
message:String

The new diagnostic context information.

Returns:CloseableThreadContext.Instance

the instance that will back out the changes when closed.

pushback to summary
public CloseableThreadContext.Instance push(final String message, final Object[] args)

Pushes new diagnostic context information on to the Thread Context Stack. The information will be popped off when the instance is closed.

Parameters
message:String

The new diagnostic context information.

args:Object[]

Parameters for the message.

Returns:CloseableThreadContext.Instance

the instance that will back out the changes when closed.

pushAllback to summary
public CloseableThreadContext.Instance pushAll(final List<String> messages)

Populates the Thread Context Stack with the supplied stack. The information will be popped off when the instance is closed.

Parameters
messages:List<String>

The list of messages to be added

Returns:CloseableThreadContext.Instance

a new instance that will back out the changes when closed.

Since
2.8
putback to summary
public CloseableThreadContext.Instance put(final String key, final String value)

Populates the Thread Context Map with the supplied key/value pair. Any existing key in the ThreadContext will be replaced with the supplied value, and restored back to their original value when the instance is closed.

Parameters
key:String

The key to be added

value:String

The value to be added

Returns:CloseableThreadContext.Instance

a new instance that will back out the changes when closed.

putAllback to summary
public CloseableThreadContext.Instance putAll(final Map<String, String> values)

Populates the Thread Context Map with the supplied key/value pairs. Any existing keys in the ThreadContext will be replaced with the supplied values, and restored back to their original value when the instance is closed.

Parameters
values:Map<String, String>

The map of key/value pairs to be added

Returns:CloseableThreadContext.Instance

a new instance that will back out the changes when closed.

Since
2.8