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:
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, ornull
if none has been set.static boolean
isEmpty()
Returnstrue
if the logging context has been initialised and has data in it, otherwisefalse
.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 isnull
.
-
setUserName
public static void setUserName(@Nullable String userName)
Sets the 'userName' value in the logging context. Sets it to 'anonymous' if userName isnull
.
-
getUserName
public static String getUserName()
Returns the currently mapped 'userName' value from the context, ornull
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 isnull
.
-
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 aString
, object equivalent of a primitive type or aJsonable
.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()
Returnstrue
if the logging context has been initialised and has data in it, otherwisefalse
.
-
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 toput(java.lang.String, java.lang.Object)
needs to be removed by callingremove(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 aString
, object equivalent of a primitive type or aJsonable
.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 contextvalue
- the value for the context. The value can either be aString
, object equivalent of a primitive type or aJsonable
.runnable
- runnable to execute- Since:
- 5.9.1
-
-