Class MDCUtils

java.lang.Object
org.pepsoft.util.mdc.MDCUtils

public final class MDCUtils extends Object
Utility methods for working with the classes and interfaces in this package.
  • Constructor Details

    • MDCUtils

      public MDCUtils()
  • Method Details

    • gatherMdcContext

      public static Map<String,String> gatherMdcContext(Throwable exception)
      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 never null.
    • doWithMdcContext

      public static <T> T doWithMdcContext(Callable<T> task, Object... context)
      Execute a task returning a result with a set of key-value pairs on the MDC log context, wrapping any exception thrown from the task in a MDCCapturingException to 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 be Strings. The values may be any type and will be converted to a string by invoking toString(). They may also be null, in which case the string "null" will be placed on the context.
      Returns:
      The result of the task.
    • decorateWithMdcContext

      public static Throwable decorateWithMdcContext(Throwable exception)
      Checks whether the MDC context has been captured somewhere on the causal chain already, and if not, wraps the exception in an MDCCapturingException or MDCCapturingRuntimeException.
      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.