Hacker News new | ask | show | jobs
by wsargent 2161 days ago
Agree with everything you've said, but MDC does have one big drawback: it's only convenient when you're working within a single thread. If you're writing asynchronous code (Scala, Akka, Vert.x etc) then MDC doesn't help you very much. [1]

I generally find it easier to have an explicit "context" object and pass that around in an operation, and then log once that context has been built up. If you're lucky, the logging framework will do that for you.

Bunyan did this the Right Way from the get go, using `log.child`. [2]

[1] https://tersesystems.github.io/blindsight/usage/context.html...

[2] https://github.com/trentm/node-bunyan#logchild

1 comments

With Kotlin, you there is a way to make the MDC work with co-routines. Basically, it works by leveraging the co-routine scope to keep track of a context. This ships as part of the co-routines library in the form of extension functions on the slf4j API: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutin...