LogContextElement

A CoroutineContext.Element that carries a LogContext through a coroutine's lifetime.

JVM and Android

Also implements ThreadContextElement<LogContext>. The coroutine dispatcher automatically calls LogContextHolder.setContext via updateThreadContext before each dispatch and restores the previous context via restoreThreadContext after. This means the LogContext is correctly visible in LogContextHolder.current() on every thread the coroutine runs on, including after suspension points that hop to a different thread-pool thread (e.g. Dispatchers.IO or Dispatchers.Default). Concurrent coroutines on JVM are also fully isolated from each other.

Other platforms (JS, WasmJS, iOS, macOS, Linux, MinGW)

kotlinx.coroutines.ThreadContextElement does not exist outside JVM, so here the element is a plain carrier: it holds the LogContext in the coroutine context and nothing mirrors it into LogContextHolder. The context still survives suspension and thread hops

  • it travels with the coroutine - and coroutines remain isolated from each other. It is simply not visible to a non-suspending LogContextHolder.current() read, because resolving the coroutine context requires suspend.

Read it with currentLogContext, or attach it to a logger with withActiveLogContext.

Accessing the element

The active element is always retrievable from inside a withLogContext block:

withLogContext(LogContext(mapOf("requestId" to "req-1"))) {
val element = kotlinx.coroutines.currentCoroutineContext()[LogContextElement]
println(element?.context) // LogContext(values={requestId=req-1})
}

Prefer withLogContext over constructing this element directly. Direct construction is useful when attaching a context to an entire kotlinx.coroutines.CoroutineScope:

val scope = CoroutineScope(
Dispatchers.IO + LogContextElement(LogContext(mapOf("service" to "api")))
)

A CoroutineContext.Element that carries a LogContext through a coroutine's lifetime.

JVM and Android

Also implements ThreadContextElement<LogContext>. The coroutine dispatcher automatically calls LogContextHolder.setContext via updateThreadContext before each dispatch and restores the previous context via restoreThreadContext after. This means the LogContext is correctly visible in LogContextHolder.current() on every thread the coroutine runs on, including after suspension points that hop to a different thread-pool thread (e.g. Dispatchers.IO or Dispatchers.Default). Concurrent coroutines on JVM are also fully isolated from each other.

Other platforms (JS, WasmJS, iOS, macOS, Linux, MinGW)

kotlinx.coroutines.ThreadContextElement does not exist outside JVM, so here the element is a plain carrier: it holds the LogContext in the coroutine context and nothing mirrors it into LogContextHolder. The context still survives suspension and thread hops

  • it travels with the coroutine - and coroutines remain isolated from each other. It is simply not visible to a non-suspending LogContextHolder.current() read, because resolving the coroutine context requires suspend.

Read it with currentLogContext, or attach it to a logger with withActiveLogContext.

Accessing the element

The active element is always retrievable from inside a withLogContext block:

withLogContext(LogContext(mapOf("requestId" to "req-1"))) {
val element = kotlinx.coroutines.currentCoroutineContext()[LogContextElement]
println(element?.context) // LogContext(values={requestId=req-1})
}

Prefer withLogContext over constructing this element directly. Direct construction is useful when attaching a context to an entire kotlinx.coroutines.CoroutineScope:

val scope = CoroutineScope(
Dispatchers.IO + LogContextElement(LogContext(mapOf("service" to "api")))
)

A CoroutineContext.Element that carries a LogContext through a coroutine's lifetime.

JVM and Android

Also implements ThreadContextElement<LogContext>. The coroutine dispatcher automatically calls LogContextHolder.setContext via updateThreadContext before each dispatch and restores the previous context via restoreThreadContext after. This means the LogContext is correctly visible in LogContextHolder.current() on every thread the coroutine runs on, including after suspension points that hop to a different thread-pool thread (e.g. Dispatchers.IO or Dispatchers.Default). Concurrent coroutines on JVM are also fully isolated from each other.

Other platforms (JS, WasmJS, iOS, macOS, Linux, MinGW)

kotlinx.coroutines.ThreadContextElement does not exist outside JVM, so here the element is a plain carrier: it holds the LogContext in the coroutine context and nothing mirrors it into LogContextHolder. The context still survives suspension and thread hops

  • it travels with the coroutine - and coroutines remain isolated from each other. It is simply not visible to a non-suspending LogContextHolder.current() read, because resolving the coroutine context requires suspend.

Read it with currentLogContext, or attach it to a logger with withActiveLogContext.

Accessing the element

The active element is always retrievable from inside a withLogContext block:

withLogContext(LogContext(mapOf("requestId" to "req-1"))) {
val element = kotlinx.coroutines.currentCoroutineContext()[LogContextElement]
println(element?.context) // LogContext(values={requestId=req-1})
}

Prefer withLogContext over constructing this element directly. Direct construction is useful when attaching a context to an entire kotlinx.coroutines.CoroutineScope:

val scope = CoroutineScope(
Dispatchers.IO + LogContextElement(LogContext(mapOf("service" to "api")))
)

A CoroutineContext.Element that carries a LogContext through a coroutine's lifetime.

JVM and Android

Also implements ThreadContextElement<LogContext>. The coroutine dispatcher automatically calls LogContextHolder.setContext via updateThreadContext before each dispatch and restores the previous context via restoreThreadContext after. This means the LogContext is correctly visible in LogContextHolder.current() on every thread the coroutine runs on, including after suspension points that hop to a different thread-pool thread (e.g. Dispatchers.IO or Dispatchers.Default). Concurrent coroutines on JVM are also fully isolated from each other.

Other platforms (JS, WasmJS, iOS, macOS, Linux, MinGW)

kotlinx.coroutines.ThreadContextElement does not exist outside JVM, so here the element is a plain carrier: it holds the LogContext in the coroutine context and nothing mirrors it into LogContextHolder. The context still survives suspension and thread hops

  • it travels with the coroutine - and coroutines remain isolated from each other. It is simply not visible to a non-suspending LogContextHolder.current() read, because resolving the coroutine context requires suspend.

Read it with currentLogContext, or attach it to a logger with withActiveLogContext.

Accessing the element

The active element is always retrievable from inside a withLogContext block:

withLogContext(LogContext(mapOf("requestId" to "req-1"))) {
val element = kotlinx.coroutines.currentCoroutineContext()[LogContextElement]
println(element?.context) // LogContext(values={requestId=req-1})
}

Prefer withLogContext over constructing this element directly. Direct construction is useful when attaching a context to an entire kotlinx.coroutines.CoroutineScope:

val scope = CoroutineScope(
Dispatchers.IO + LogContextElement(LogContext(mapOf("service" to "api")))
)

Kotlin/Native implementation, shared by iOS, macOS, Linux, and MinGW.

kotlinx.coroutines.ThreadContextElement does not exist on Kotlin/Native, so this element cannot reinstall the context when a coroutine resumes. It is a plain carrier: the context travels with the coroutine, and is read via currentLogContext or bound to a logger with withActiveLogContext.

A CoroutineContext.Element that carries a LogContext through a coroutine's lifetime.

JVM and Android

Also implements ThreadContextElement<LogContext>. The coroutine dispatcher automatically calls LogContextHolder.setContext via updateThreadContext before each dispatch and restores the previous context via restoreThreadContext after. This means the LogContext is correctly visible in LogContextHolder.current() on every thread the coroutine runs on, including after suspension points that hop to a different thread-pool thread (e.g. Dispatchers.IO or Dispatchers.Default). Concurrent coroutines on JVM are also fully isolated from each other.

Other platforms (JS, WasmJS, iOS, macOS, Linux, MinGW)

kotlinx.coroutines.ThreadContextElement does not exist outside JVM, so here the element is a plain carrier: it holds the LogContext in the coroutine context and nothing mirrors it into LogContextHolder. The context still survives suspension and thread hops

  • it travels with the coroutine - and coroutines remain isolated from each other. It is simply not visible to a non-suspending LogContextHolder.current() read, because resolving the coroutine context requires suspend.

Read it with currentLogContext, or attach it to a logger with withActiveLogContext.

Accessing the element

The active element is always retrievable from inside a withLogContext block:

withLogContext(LogContext(mapOf("requestId" to "req-1"))) {
val element = kotlinx.coroutines.currentCoroutineContext()[LogContextElement]
println(element?.context) // LogContext(values={requestId=req-1})
}

Prefer withLogContext over constructing this element directly. Direct construction is useful when attaching a context to an entire kotlinx.coroutines.CoroutineScope:

val scope = CoroutineScope(
Dispatchers.IO + LogContextElement(LogContext(mapOf("service" to "api")))
)

Constructors

Link copied to clipboard
actual constructor(context: LogContext)
expect constructor(context: LogContext)
actual constructor(context: LogContext)
actual constructor(context: LogContext)
actual constructor(context: LogContext)
actual constructor(context: LogContext)

Types

Link copied to clipboard

Properties

Link copied to clipboard
actual val context: LogContext
expect val context: LogContext
actual val context: LogContext
actual val context: LogContext
actual val context: LogContext
actual val context: LogContext
Link copied to clipboard
expect open override val key: CoroutineContext.Key<*>
open override val key: CoroutineContext.Key<*>
open override val key: CoroutineContext.Key<*>
open override val key: CoroutineContext.Key<*>

Functions

fold
Link copied to clipboard
open override fun <R> fold(initial: R, operation: (R, CoroutineContext.Element) -> R): R
open override fun <R> fold(initial: R, operation: (R, CoroutineContext.Element) -> R): R
open override fun <R> fold(initial: R, operation: (R, CoroutineContext.Element) -> R): R
open override fun <R> fold(initial: R, operation: (R, CoroutineContext.Element) -> R): R
open override fun <R> fold(initial: R, operation: (R, CoroutineContext.Element) -> R): R
open override fun <R> fold(initial: R, operation: (R, CoroutineContext.Element) -> R): R
get
Link copied to clipboard
open operator override fun <E : CoroutineContext.Element> get(key: CoroutineContext.Key<E>): E?
open operator override fun <E : CoroutineContext.Element> get(key: CoroutineContext.Key<E>): E?
open operator override fun <E : CoroutineContext.Element> get(key: CoroutineContext.Key<E>): E?
open operator override fun <E : CoroutineContext.Element> get(key: CoroutineContext.Key<E>): E?
open operator override fun <E : CoroutineContext.Element> get(key: CoroutineContext.Key<E>): E?
open operator override fun <E : CoroutineContext.Element> get(key: CoroutineContext.Key<E>): E?
minusKey
Link copied to clipboard
open override fun minusKey(key: CoroutineContext.Key<*>): CoroutineContext
open override fun minusKey(key: CoroutineContext.Key<*>): CoroutineContext
open override fun minusKey(key: CoroutineContext.Key<*>): CoroutineContext
open override fun minusKey(key: CoroutineContext.Key<*>): CoroutineContext
open override fun minusKey(key: CoroutineContext.Key<*>): CoroutineContext
open override fun minusKey(key: CoroutineContext.Key<*>): CoroutineContext
plus
Link copied to clipboard
open operator fun plus(context: CoroutineContext): CoroutineContext
open operator fun plus(context: CoroutineContext): CoroutineContext
open operator fun plus(context: CoroutineContext): CoroutineContext
open operator fun plus(context: CoroutineContext): CoroutineContext
open operator fun plus(context: CoroutineContext): CoroutineContext
open operator fun plus(context: CoroutineContext): CoroutineContext
Link copied to clipboard
open override fun restoreThreadContext(context: CoroutineContext, oldState: LogContext)
open override fun restoreThreadContext(context: CoroutineContext, oldState: LogContext)
Link copied to clipboard
open override fun updateThreadContext(context: CoroutineContext): LogContext
open override fun updateThreadContext(context: CoroutineContext): LogContext