withActiveLogContext
Returns a Logger bound to the coroutine's active LogContext.
This is the portable way to log inside a withLogContext block: the context is read from the coroutine context and bound to the logger object, so every event it emits carries the context on every platform.
withLogContext(LogContext(mapOf("requestId" to id))) {
val log = LoggerFactory.get("Api").withActiveLogContext()
log.info { "Starting" } // carries requestId on every platform
withContext(Dispatchers.Default) {
log.debug { "Working" } // still carries it - it is bound to the object
}
}Content copied to clipboard
On JVM/Android the ambient context is also visible to plain, unbound loggers (see withLogContext); binding additionally works everywhere else.