MDC

object MDC(source)

Mapped Diagnostic Context (MDC) for structured logging.

Provides thread-local and coroutine-context storage for contextual information that should be included with log messages.

Usage:

MDC.put("traceId", generateTraceId())
MDC.put("userId", user.id)

SoulLogger.info("Processing request")
// Output: {..., "context":{"traceId":"abc123","userId":"user456"}, ...}

MDC.clear()

Author

LesterE

Since

1.0.0

Functions

Link copied to clipboard

Create a CoroutineContext element for propagating MDC across coroutines.

Link copied to clipboard
fun clear()

Clear all values from the MDC.

Link copied to clipboard
fun get(key: String): String?

Get a value from the MDC.

Link copied to clipboard

Get a copy of the current MDC context map.

Link copied to clipboard
fun put(key: String, value: String)

Put a value into the MDC.

Link copied to clipboard
fun remove(key: String)

Remove a value from the MDC.

Link copied to clipboard
fun setContextMap(contextMap: Map<String, String>)

Set the MDC context map.

Link copied to clipboard
inline fun <T> withContext(vararg pairs: Pair<String, String>, block: () -> T): T

Execute a block with temporary MDC values.