3

I have been trying to add a trace id to my logs of micro services. I have tried using sleuth and spring cloud and it is working. But I dont want to load spring cloud just to add trace id to my log.Is it possible to add traceid to the logs without loading the spring cloud.? I mean I just want to add a traceID to the logs.

Sreyas
  • 744
  • 1
  • 7
  • 25

1 Answers1

1

one option might be using Mapped Diagnostic Context (MDC) which is supported by most logging frameworks.
Your service entrypoint generates a unique ID and pushes it to MDC: the context variable is added to each log within the same thread.
With log4j it would be something like:

MDC.put("TRACE_ID", traceId);

and corresponding log4j configuration

log4j.appender.l.layout.ConversionPattern = %d{yyyy-MM-dd HH:mm:ss} [%X{TRACE_ID}] %-5p %c -> %m%n

I believe slf4j offers similar capabilities

Beppe C
  • 11,256
  • 2
  • 19
  • 41