NeQuissimus / zio-slf4j

Referentially transparent logging with slf4j
Apache License 2.0
21 stars 8 forks source link

Mapped Diagnostic Context ? #25

Open thiloplanz opened 5 years ago

thiloplanz commented 5 years ago

Are there any plans to integrate MDC?

Because of the "classic" MDC implementation using thread locals not working very well in thread-shifting environments like ZIO (or pretty much anything else in Scalaland), having this enabled out of the box (i.e. without the user having to manually mess with fiber-locals or such) would be the killer feature for this library.

wpoosanguansit commented 5 years ago

+1

I was about to ask on how to log the trace on code that forks. I hope there is an easier way to do that with the library.

NeQuissimus commented 5 years ago

Any PRs are more than welcome. I am not super familiar with MDC but I bet it could be integrated with the tracing capabilities in ZIO

wpoosanguansit commented 5 years ago

@NeQuissimus Could you show how can we log or trace code that forks? Thanks for your help.

nigredo-tori commented 4 years ago

SLF4J 2.0 adds a new facility for attaching key-value pairs to logging events, together with LoggingEventBuilders. In the long term, this should fit this library much better than abusing thread-local MDC APIs.

NeQuissimus commented 4 years ago

@nigredo-tori Interesting, I would love to see an example of that work. PRs are always welcome, too :)

thiloplanz commented 4 years ago

@nigredo-tori Not sure if LoggingEventBuilders are useful here.

They seem to provide an alternative way to prepare/format a single log message, whereas the main use-case for MDC is to be able to split attaching context from the (usually multiple) places that produce logs.

A common example is attaching a "request id" at the top of your application that gets then included in the dozens of log messages output all over your codebase (without those places having to know about it).

It may be theoretically possible to pass around a LoggingEventBuilder, but those already fix the log level and the logger name at creation time before you can even set a key-value pair, so that neither could be changed anymore. It would also force all user code to stop using the familiar "old" API (log.debug("something")) if they want to pick up the context values.

thiloplanz commented 4 years ago

There is now a proposal for an "official" ZIO Logging interface with pluggable backends (one of which could be this one, I suppose), and they are discussing providing a context object there (which would presumably be implemented using FiberRef and be used for Mapped Diagnostic Context such as correlation ids).

https://github.com/zio/zio-logging/issues/1#issuecomment-529912701