futevolei
futevolei

Reputation: 190

Spring-Cloud-Sleuth enable MDC properties in pattern layout to log TraceId

I am not seeing the TraceId or SpanId in our log files after adding the spring-cloud-sleuth dependency. We use log4j2 and slf4j. Apparently this works out of the box with logback but not log4j2. It seems other people have had success using the json layout by adding properties=true in the config file but we use pattern layout and it doesn't seem that boolean is available. I have configured the pattern correctly but that doesn't matter as when I set the debugger in the MDCPatternConverter class, the thread context map is empty. I can make code changes and directly set MDC values but people downstream from us want us to use this dependency for some reason.

Added log4j-JUL Added spring-boot-starter-log4j2

<Property name="STP_PATTERN">%d{yyyy-MM-dd HH:mm:ss,SSS zzz} %-5p  [%X{X-B3-TraceId} TEST %X{X-B3-SpanId} %t:%c{1}:%x] -%m%n
    </Property>

Upvotes: 2

Views: 6229

Answers (3)

Mostafa Hassan
Mostafa Hassan

Reputation: 298

All you need to do is the following:

  • add io.zipkin.brave:brave-context-log4j2 dependency

  • Add org.springframework.boot:spring-boot-starter-log4j2 dependency

  • Add org.springframework.cloud:spring-cloud-starter-sleuth dependency

  • Exclude default spring logging

     <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-web</artifactId>
         <exclusions>
             <exclusion>
                 <groupId>org.springframework.boot</groupId>
                 <artifactId>spring-boot-starter-logging</artifactId>
             </exclusion>
         </exclusions>
     </dependency>
    
  • in your log4j2.xml adjust the pattern layout

    <PatternLayout pattern="[%d{yyy-MM-dd HH:mm:ss:SSS}] [%X{traceId}, %X{spanId}] {%-5level} - %l - %m%n" />
    

Upvotes: 0

Marcin Grzejszczak
Marcin Grzejszczak

Reputation: 11189

Sleuth uses Brave under the hook. You have to add the io.zipkin.brave:brave-context-log4j2 dependency to your classpath. Please check out Brave's documentation for more information https://github.com/openzipkin/brave/tree/master/context/log4j2 I'll copy it here for your convenience

This adds trace and span IDs to the Log4J 2 Thread Context so that you can search or aggregate logs accordingly.

To enable this, configure brave.Tracing with ThreadContextScopeDecorator like so:

Just create a bean of ThreadContextScopeDecorator type and Sleuth will pick it up

@Bean
ScopeDecorator threadContextScopeDecorator() {
return new ThreadContextScopeDecorator();
}

Upvotes: 3

fedup
fedup

Reputation: 1259

If you want to log within spawned threads you need to copy the MDC thread local context from the current thread into the new one.

In the main thread...

 final Map<String, String> threadContext = MDC.getCopyOfContextMap();
 Runnable mythread = () -> { MDC.setContextMap(threadContext); 
                             yourCode(); };
 new Thread(mythread).start();

Upvotes: 0

Related Questions