From the documentation I found out that in order to add extra MDC fields I need to use a DiagnosticLoggingAdapter. It’s clearly explained how to instantiate one when you’re in an AbstractActor context. However, I can’t figure out how to instantiate one outside of it.
In particular, I am using Akka http. When I extract the logger I get back a LoggingAdapter, hence I cannot directly add extra fields to the MDC.
What am I missing? How do people do this?
Many thanks
I’m afraid that’s not currently possible with the extractLog
directive, furthermore I think that logger is shared with all concurrent requests which is likely not where you want to put MDC values.
As far as I can see you’d currently have to create your own directive providing a LoggingAdapter
that is not shared between requests and use that to use MDC with the routing dsl.
that’s a shame, but not the end of the world. I guess the other question is: how do I create a DiagnosticLoggingAdapter outside of an AbstractActor?
I can see there is no obvious API for that, you could either use SLF4J or the underlying logging API directly (but make sure the appender is async), or mimic the logic inside of this factory method: https://github.com/akka/akka/blob/master/akka-actor/src/main/scala/akka/event/Logging.scala#L585
I’m pretty sure all the things used in there are public APIs.
Actually, you can probably use the Logger.withMarker(bus, logSource)
factory as that logger already has the DiagnosticLoggingAdapter