Package org.pepsoft.util.mdc
Class MDCUtils
java.lang.Object
org.pepsoft.util.mdc.MDCUtils
Utility methods for working with the classes and interfaces in this package.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic ThrowabledecorateWithMdcContext(Throwable exception) Checks whether the MDC context has been captured somewhere on the causal chain already, and if not, wraps the exception in anMDCCapturingExceptionorMDCCapturingRuntimeException.static <T> TdoWithMdcContext(Callable<T> task, Object... context) Execute a task returning a result with a set of key-value pairs on theMDClog context, wrapping any exception thrown from the task in aMDCCapturingExceptionto capture the full current MDC context if necessary.gatherMdcContext(Throwable exception) Finds all MDC context information in the causal chain and combines them into one map.
-
Constructor Details
-
MDCUtils
public MDCUtils()
-
-
Method Details
-
gatherMdcContext
Finds all MDC context information in the causal chain and combines them into one map. If a key occurs more than once with different values, the values are concatenated together, separated by commas.- Parameters:
exception- The exception for which to gather the MDC context information.- Returns:
- All MDC context information from the causal chain combined
together. May be an empty
Map, but nevernull.
-
doWithMdcContext
Execute a task returning a result with a set of key-value pairs on theMDClog context, wrapping any exception thrown from the task in aMDCCapturingExceptionto capture the full current MDC context if necessary.- Parameters:
task- The task to execute.context- An interleaved list of keys and values. Must have an even length. The keys must beStrings. The values may be any type and will be converted to a string by invokingtoString(). They may also benull, in which case the string"null"will be placed on the context.- Returns:
- The result of the task.
-
decorateWithMdcContext
Checks whether the MDC context has been captured somewhere on the causal chain already, and if not, wraps the exception in anMDCCapturingExceptionorMDCCapturingRuntimeException.- Parameters:
exception- The exception to check for the presence of a captured MDC context.- Returns:
- An exception that is guaranteed to have the MDC context of the current thread somewhere on the chain.
-