Class LoggingContext

java.lang.Object
com.atlassian.confluence.util.logging.LoggingContext

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

    Fields
    Modifier and Type
    Field
    Description
    static final String
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static void
    Removes all keys from the logging context which are managed by this class.
    static void
    Clears all MDC information on the current thread.
    static void
     
    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
    Execute Runnable with MDC context that is removed once the runnable returns.
    static Map<String,Object>
    Returns an immutable copy of the underlying logging context map.
    static String
    Returns the currently mapped 'traceId' value from the context, or null if none has been set.
    static String
    Returns the currently mapped 'userName' value from the context, or null if none has been set.
    static boolean
    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.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

  • Method Details

    • 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.
    • getTraceId

      public static String getTraceId()
      Returns the currently mapped 'traceId' value from the context, or null if none has been set.
    • 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.
    • clear

      public static void clear()
      Removes all keys from the logging context which are managed by this class. Anything which has been added by a call to put(java.lang.String, java.lang.Object) needs to be removed by calling remove(java.lang.String...) with the same key.
    • 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