Skip to content

Commit 6ff2f9a

Browse files
committed
docs(jvm): add docs explaining withCoroutineLoggingContext
1 parent fbc9f5f commit 6ff2f9a

1 file changed

Lines changed: 16 additions & 0 deletions

File tree

  • src/jvmTest/kotlin/io/github/oshai/kotlinlogging/coroutines

src/jvmTest/kotlin/io/github/oshai/kotlinlogging/coroutines/README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,19 @@ withLoggingContextAsync("userId" to "ANOTHER_USER_ID", restorePrevious = false)
1717
// The MDC context will retain the mapping of "userId"=>"ANOTHER_USER_ID",
1818
// as the previous context restoration was disabled.
1919
```
20+
21+
## Coroutine-Safe wrappers
22+
23+
If you prefer to leverage coroutine context instead of thread local storage, you can use the `withCoroutineLoggingContext` wrapper.
24+
This will NOT inherit the current MDC set prior, but will carry existing coroutine context within nested declarations, restoring previous context
25+
at the logical coroutine level rather than thread level.
26+
27+
```kotlin
28+
MDC.put("foo", "bar")
29+
withCoroutineLoggingContext("userId" to "A_USER_ID") {
30+
// The MDC context will contain the mapping of "userId"=>"A_USER_ID"
31+
// during this log statement, but NOT the mapping of "foo"=>"bar" as the coroutine context does not inherit the thread local MDC.
32+
logger.info { "..." }
33+
}
34+
// after the block, the MDC will now only contain "foo"=>"bar"
35+
```

0 commit comments

Comments
 (0)