Class BasicMDCAdapter

  • All Implemented Interfaces:
    MDCAdapter

    public class BasicMDCAdapter
    extends java.lang.Object
    implements MDCAdapter
    Basic MDC implementation, which can be used with logging systems that lack out-of-the-box MDC support. This code was initially inspired by logback's LogbackMDCAdapter. However, LogbackMDCAdapter has evolved and is now considerably more sophisticated.
    Since:
    1.5.0
    Author:
    Ceki Gulcu, Maarten Bosteels, Lukasz Cwik
    • Constructor Summary

      Constructors 
      Constructor Description
      BasicMDCAdapter()  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void clear()
      Clear all entries in the MDC.
      void clearDequeByKey​(java.lang.String key)
      Clear the deque(stack) referenced by 'key'.
      java.lang.String get​(java.lang.String key)
      Get the context identified by the key parameter.
      java.util.Map<java.lang.String,​java.lang.String> getCopyOfContextMap()
      Return a copy of the current thread's context map.
      java.util.Deque<java.lang.String> getCopyOfDequeByKey​(java.lang.String key)
      Returns a copy of the deque(stack) referenced by 'key'.
      java.util.Set<java.lang.String> getKeys()
      Returns the keys in the MDC as a Set of Strings The returned value can be null.
      java.lang.String popByKey​(java.lang.String key)
      Pop the stack referenced by 'key' and return the value possibly null.
      void pushByKey​(java.lang.String key, java.lang.String value)
      Push a value into the deque(stack) referenced by 'key'.
      void put​(java.lang.String key, java.lang.String val)
      Put a context value (the val parameter) as identified with the key parameter into the current thread's context map.
      void remove​(java.lang.String key)
      Remove the context identified by the key parameter.
      void setContextMap​(java.util.Map<java.lang.String,​java.lang.String> contextMap)
      Set the current thread's context map by first clearing any existing map and then copying the map passed as parameter.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • put

        public void put​(java.lang.String key,
                        java.lang.String val)
        Put a context value (the val parameter) as identified with the key parameter into the current thread's context map. Note that contrary to log4j, the val parameter can be null.

        If the current thread does not have a context map it is created as a side effect of this call.

        Specified by:
        put in interface MDCAdapter
        Throws:
        java.lang.IllegalArgumentException - in case the "key" parameter is null
      • get

        public java.lang.String get​(java.lang.String key)
        Get the context identified by the key parameter.
        Specified by:
        get in interface MDCAdapter
        Returns:
        the string value identified by the key parameter.
      • remove

        public void remove​(java.lang.String key)
        Remove the context identified by the key parameter.
        Specified by:
        remove in interface MDCAdapter
      • clear

        public void clear()
        Clear all entries in the MDC.
        Specified by:
        clear in interface MDCAdapter
      • getKeys

        public java.util.Set<java.lang.String> getKeys()
        Returns the keys in the MDC as a Set of Strings The returned value can be null.
        Returns:
        the keys in the MDC
      • getCopyOfContextMap

        public java.util.Map<java.lang.String,​java.lang.String> getCopyOfContextMap()
        Return a copy of the current thread's context map. Returned value may be null.
        Specified by:
        getCopyOfContextMap in interface MDCAdapter
        Returns:
        A copy of the current thread's context map. May be null.
      • setContextMap

        public void setContextMap​(java.util.Map<java.lang.String,​java.lang.String> contextMap)
        Description copied from interface: MDCAdapter
        Set the current thread's context map by first clearing any existing map and then copying the map passed as parameter. The context map parameter must only contain keys and values of type String. Implementations must support null valued map passed as parameter.
        Specified by:
        setContextMap in interface MDCAdapter
        Parameters:
        contextMap - must contain only keys and values of type String
      • pushByKey

        public void pushByKey​(java.lang.String key,
                              java.lang.String value)
        Description copied from interface: MDCAdapter
        Push a value into the deque(stack) referenced by 'key'.
        Specified by:
        pushByKey in interface MDCAdapter
        Parameters:
        key - identifies the appropriate stack
        value - the value to push into the stack
      • popByKey

        public java.lang.String popByKey​(java.lang.String key)
        Description copied from interface: MDCAdapter
        Pop the stack referenced by 'key' and return the value possibly null.
        Specified by:
        popByKey in interface MDCAdapter
        Parameters:
        key - identifies the deque(stack)
        Returns:
        the value just popped. May be null/
      • getCopyOfDequeByKey

        public java.util.Deque<java.lang.String> getCopyOfDequeByKey​(java.lang.String key)
        Description copied from interface: MDCAdapter
        Returns a copy of the deque(stack) referenced by 'key'. May be null.
        Specified by:
        getCopyOfDequeByKey in interface MDCAdapter
        Parameters:
        key - identifies the stack
        Returns:
        copy of stack referenced by 'key'. May be null.
      • clearDequeByKey

        public void clearDequeByKey​(java.lang.String key)
        Description copied from interface: MDCAdapter
        Clear the deque(stack) referenced by 'key'.
        Specified by:
        clearDequeByKey in interface MDCAdapter
        Parameters:
        key - identifies the stack