File tree Expand file tree Collapse file tree
src/jvmTest/kotlin/io/github/oshai/kotlinlogging/coroutines Expand file tree Collapse file tree Original file line number Diff line number Diff 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+ ```
You can’t perform that action at this time.
0 commit comments