withLogContext
Execute block with the given entries added to the current thread's context.
The entries are added before block runs and removed (or restored to their previous values) after block completes, even if it throws an exception. This prevents context leaks compared to manual putLogContext / removeLogContext pairs.
Thread constraint: Because the context is backed by a ThreadLocal, block must complete entirely on the calling thread. Do not switch coroutine dispatchers (e.g., kotlinx.coroutines.withContext(Dispatchers.IO)) inside block; context entries will not be visible on the new thread.
StructuredLog.withLogContext("request_id" to requestId, "user_id" to userId) {
// All logs within this block include request_id and user_id.
StructuredTimber.d("Processing request", "action" to "checkout")
}
// request_id and user_id are automatically removed here.Return
The result of block.
Since
2.0.0
Parameters
Key-value pairs to add to the context for the duration of block.
The block to execute. Scoped entries are available to all StructuredTimber calls made on this thread within block; no argument is passed.