'TraceID in Microservice Logs
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.
Solution 1:[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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Beppe C |