Spent some time tracking down this logging bug involving withLoggingContext and coroutines and wanted to make sure no one else tripped over it.
I had some code written as such:
withLoggingContext("foo" to "bar") {
withContext(MDCContext()) { suspending_function() }
}
The problem is that you may be in a different thread when you leave suspending_function as when you entered it, so the MDC.remove call won't have an effect. The wiki example code uses launch, so doesn't have this problem.
My workaround was:
val mdc = MDC.getCopyOfContextMap()?.apply { put("foo", "bar") } ?: mapOf("foo" to "bar")
withContext<T>(MDCContext(mdc)) { suspending_function() }
But I rather have something as nice as withLoggingContext. I'd be happy to submit a patch, but I wanted some direction on the API. Do we use a different function entirely, or should withLoggingContext take a flag where that flags the block as suspending? You could argue that this belongs in the slf4j integration directly and I should ping those guys.
Spent some time tracking down this logging bug involving withLoggingContext and coroutines and wanted to make sure no one else tripped over it.
I had some code written as such:
The problem is that you may be in a different thread when you leave suspending_function as when you entered it, so the MDC.remove call won't have an effect. The wiki example code uses launch, so doesn't have this problem.
My workaround was:
But I rather have something as nice as withLoggingContext. I'd be happy to submit a patch, but I wanted some direction on the API. Do we use a different function entirely, or should withLoggingContext take a flag where that flags the block as suspending? You could argue that this belongs in the slf4j integration directly and I should ping those guys.