Class MDCSnapshot

  • All Implemented Interfaces:
    Closeable, AutoCloseable

    public final class MDCSnapshot
    extends Object
    implements Closeable
    Takes a 'snapshot' of the current MDC context map which is restored on (auto) close. This can be used to ensure that no MDC values set (or overridden) inside of call stack will "leak" beyond that call stack.
     try (var mdcSnapshot = MDCSnapshot.create()) {
       assert null != mdcSnapshot; // prevent compiler warning
    
       // ... arbitrary calls to MDC methods
     }
     
    See Also:
    MDC.putCloseable(java.lang.String, java.lang.String)