https://kotlinlang.org logo
Title
n

Nikky

11/12/2020, 4:25 PM
how can i retrieve MDC from a CoroutineContext in a exception handler ?  the code looks like so currently
private val exceptionHandler = CoroutineExceptionHandler { context, e -> GlobalScope.launch(MDCContext() + context) {
can i somehow do 
context.getCopyOfContextMap()
  ?
l

louiscad

11/12/2020, 5:10 PM
It's unclear what MDC is, but docs about CoroutineContext online explain how to put custom stuff in it and how to get it back.
g

gildor

11/13/2020, 6:33 AM
MDC is Mapped Diagnostic Context, integration with sl4j logging
how can i retrieve MDC from a CoroutineContext in a exception handler
Maybe I misunderstood your case, but why not just
CoroutineExceptionHandler { context, e ->
val mdcMap = 
context[MDCContext]?.contextMap
}
n

Nikky

11/13/2020, 12:00 PM
i arrived at this.. and i hope it ends up working..
MDCContext(context[MDCContext.Key]?.contextMap)
g

gildor

11/13/2020, 1:01 PM
So your goal is copy it? Why not just use directly?
n

Nikky

11/13/2020, 3:13 PM
the goal is to only extract the MDCContext out of the
context
variable so that the logged error will have the mdc context from where the exception comes.. hopefully