[slf4j-user] logging common data simply

Joachim Durchholz jo at durchholz.org
Sat Feb 25 21:15:46 CET 2017


On 25.02.2017 20:34, Shanholtz, Jeff wrote:
> I have a scenario where I have 1, 2, or 3 pieces of data (guids) which I
> want to to log with all messages in order to track records as they
> traverse their way through a series of microservices.

That's a common scenario.

> Currently I’ve written my own wrapper class with my own debug, info,
> warn, error methods that take these 3 parameters, followed by the
> message field, followed by the optional parameters.

Any library function that you call will still use the original loggers 
so you won't know with which of the guids some log message from deep 
inside a call is associated.
This is a real PITA if you have multiple requests executing in parallel.

> There must be a better way, plus this has the disadvantage of rendering
> the line number useless in log messages because the line numbers are the
> line numbers from my log wrapper class, not the actual code doing the
> logging.

Elliot has the start of the trail right: Use an MDC.
The other half of the trail is that formatters have an escape code for 
extracting data from the MDC (%m if I remember correctly).

HTH
Jo


More information about the slf4j-user mailing list