Class LoggingContext


  • @ParametersAreNonnullByDefault
    public final class LoggingContext
    extends Object
    Manages contextual information in the Log4J Mapped Diagnostic Context.
    Since:
    3.5
    See Also:
    LoggingContextFilter, MDC
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void clear()
      Removes all keys from the logging context which are managed by this class.
      static void clearAll()
      Clears all MDC information on the current thread.
      static void clearUsername()  
      static void executeWithContext​(String key, Object value, Runnable runnable)
      Execute Runnable with supplied key/value in the MDC context that are removed once the runnable returns.
      static void executeWithContext​(Map<String,​Object> context, Runnable runnable)
      Execute Runnable with MDC context that is removed once the runnable returns.
      static Map<String,​Object> getContext()
      Returns an immutable copy of the underlying logging context map.
      static String getUserName()
      Returns the currently mapped 'userName' value from the context, or null if none has been set.
      static boolean isEmpty()
      Returns true if the logging context has been initialised and has data in it, otherwise false.
      static void put​(String key, Object value)
      Sets the value associated with the key in the logging context, overwriting any existing value.
      static void remove​(String... keys)
      Removes any value associated with the keys in the logging context.
      static void setReferer​(@Nullable String referer)
      Sets the 'referer' value in the logging context.
      static void setUrl​(@Nullable String url)
      Sets the 'url' value in the logging context.
      static void setUserName​(@Nullable String userName)
      Sets the 'userName' value in the logging context.
    • Method Detail

      • setUrl

        public static void setUrl​(@Nullable String url)
        Sets the 'url' value in the logging context. Does nothing if url is null.
      • setUserName

        public static void setUserName​(@Nullable String userName)
        Sets the 'userName' value in the logging context. Sets it to 'anonymous' if userName is null.
      • getUserName

        public static String getUserName()
        Returns the currently mapped 'userName' value from the context, or null if none has been set.
      • setReferer

        public static void setReferer​(@Nullable String referer)
        Sets the 'referer' value in the logging context. Does nothing if referer is null.
      • put

        public static void put​(String key,
                               Object value)
        Sets the value associated with the key in the logging context, overwriting any existing value. The value can either be a String, object equivalent of a primitive type or a Jsonable.

        Code which calls to this method should remove the context information by calling remove(java.lang.String...) with the same key in a finally block.

        See Also:
        MDC.put(java.lang.String, java.lang.String)
      • remove

        public static void remove​(String... keys)
        Removes any value associated with the keys in the logging context.
      • isEmpty

        public static boolean isEmpty()
        Returns true if the logging context has been initialised and has data in it, otherwise false.
      • getContext

        public static Map<String,​Object> getContext()
        Returns an immutable copy of the underlying logging context map.
      • clearUsername

        public static void clearUsername()
      • clearAll

        public static void clearAll()
        Clears all MDC information on the current thread. Normally used to clear nested MDC information on a new thread.
      • executeWithContext

        public static void executeWithContext​(Map<String,​Object> context,
                                              Runnable runnable)
        Execute Runnable with MDC context that is removed once the runnable returns.
        Parameters:
        context - context to have in MDC while runnable is executed. The values can either be a String, object equivalent of a primitive type or a Jsonable.
        runnable - runnable to execute
        Since:
        5.7
      • executeWithContext

        public static void executeWithContext​(String key,
                                              Object value,
                                              Runnable runnable)
        Execute Runnable with supplied key/value in the MDC context that are removed once the runnable returns.
        Parameters:
        key - the key for the context
        value - the value for the context. The value can either be a String, object equivalent of a primitive type or a Jsonable.
        runnable - runnable to execute
        Since:
        5.9.1