简体   繁体   English

Spring-Cloud-Sleuth 在模式布局中启用 MDC 属性以记录 TraceId

[英]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.添加 spring-cloud-sleuth 依赖项后,我没有在我们的日志文件中看到 TraceId 或 SpanId。 We use log4j2 and slf4j.我们使用 log4j2 和 slf4j。 Apparently this works out of the box with logback but not log4j2.显然,这适用于 logback 但不适用于 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.似乎其他人通过在配置文件中添加 properties=true 来成功使用 json 布局,但我们使用模式布局,似乎 boolean 不可用。 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.我已经正确配置了模式,但这并不重要,因为当我在 MDCPatternConverter class 中设置调试器时,线程上下文 map 为空。 I can make code changes and directly set MDC values but people downstream from us want us to use this dependency for some reason.我可以更改代码并直接设置 MDC 值,但我们下游的人出于某种原因希望我们使用此依赖项。

Added log4j-JUL Added spring-boot-starter-log4j2添加了 log4j-JUL 添加了 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>

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. 如果要在生成的线程中登录,则需要将MDC线程本地上下文从当前线程复制到新线程中。

In the main thread... 在主线程中...

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

Sleuth uses Brave under the hook. 侦探在钩下使用勇敢。 You have to add the io.zipkin.brave:brave-context-log4j2 dependency to your classpath. 您必须将io.zipkin.brave:brave-context-log4j2依赖项添加到您的类路径中。 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 请查看Brave的文档以获取更多信息https://github.com/openzipkin/brave/tree/master/context/log4j2为了方便起见,我将其复制在此处

This adds trace and span IDs to the Log4J 2 Thread Context so that you can search or aggregate logs accordingly. 这会将跟踪和跨度ID添加到Log4J 2线程上下文中,以便您可以相应地搜索或聚合日志。

To enable this, configure brave.Tracing with ThreadContextScopeDecorator like so: 要启用此功能,请使用ThreadContextScopeDecorator配置brave.Tracing,如下所示:

Just create a bean of ThreadContextScopeDecorator type and Sleuth will pick it up 只需创建一个ThreadContextScopeDecorator类型的bean,Sleuth就会选择它

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

All you need to do is the following:您需要做的就是以下几点:

  • add io.zipkin.brave:brave-context-log4j2 dependency添加 io.zipkin.brave:brave-context-log4j2 依赖

  • Add org.springframework.boot:spring-boot-starter-log4j2 dependency添加 org.springframework.boot:spring-boot-starter-log4j2 依赖

  • Add org.springframework.cloud:spring-cloud-starter-sleuth dependency添加 org.springframework.cloud:spring-cloud-starter-sleuth 依赖

  • Exclude default spring logging排除默认 spring 日志记录

     <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在您的 log4j2.xml 中调整图案布局

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM