问题描述
我有基于 Spring Boot 的 Web 应用程序,它使用 logback 进行日志记录.
I have web application based on Spring Boot and it uses logback for logging.
我还使用以下方法从 Spring Boot 继承了一些 logback 默认值:
I also inherit some logback defaults from spring boot using:
<include resource="org/springframework/boot/logging/logback/base.xml"/>
我想开始记录跟踪信息,所以我添加了:
I want to start logging tracing information, so I added:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-sleuth</artifactId>
</dependency>
Sleuth 向日志行添加了跟踪信息,但我在模式中找不到任何 %X
或 %mdc
:https://github.com/spring-projects/spring-boot/blob/2.3.x/spring-boot-project/spring-boot/src/main/resources/org/springframework/boot/logging/logback/defaults.xml
Sleuth adds tracing information to log lines, but I can't find any %X
or %mdc
in patterns: https://github.com/spring-projects/spring-boot/blob/2.3.x/spring-boot-project/spring-boot/src/main/resources/org/springframework/boot/logging/logback/defaults.xml
Sleuth 如何将跟踪信息添加到日志行中?
How does Sleuth add tracing information into log lines?
我使用 spring-cloud-starter-parent
Hoxton.SR9
parent,它带来了 Spring Boot 2.3.5.RELEASE
和 spring-cloud-starter-sleuth
2.2.6.RELEASE
I use spring-cloud-starter-parent
Hoxton.SR9
parent which brings Spring Boot 2.3.5.RELEASE
and spring-cloud-starter-sleuth
2.2.6.RELEASE
推荐答案
(tl;dr at the bottom)
从这个问题我想你已经发现 traceId
和 spanId
被放入 MDC.
(tl;dr at the bottom)
From the question I suppose you already figured out that the traceId
and spanId
are placed into the MDC.
如果你看看 sleuth docs 的日志集成部分 您将看到示例中的跟踪信息介于日志级别 (ERROR
) 和 pid (97192
) 之间).如果您尝试将其与 logback config 你会看到日志级别和pid之间没有任何东西:${LOG_LEVEL_PATTERN:-%5p} ${PID:- }
那么跟踪信息如何到达那里可能是一个有效的问题.
If you take a look at the log integration section of the sleuth docs you will see that the tracing info in the example is between the log level (ERROR
) and the pid (97192
). If you try to match this with the logback config you will see that there is nothing between the log level and the pid: ${LOG_LEVEL_PATTERN:-%5p} ${PID:- }
so how the tracing information get there could be a valid question.
如果你再看一下文档,它是这样写的:
If you take another look to the docs, it says this:
此日志配置由 Sleuth 自动设置.您可以通过 spring.sleuth.enabled=false
属性禁用 Sleuth 或放置自己的 logging.pattern.level
属性来禁用它.
仍然没有明确解释机制,但它给了你一个巨大的提示:
Which still not explicitly explains the mechanism but it gives you a huge hint:
放置你自己的logging.pattern.level
属性
基于此,您可能会认为日志级别和 pid 之间没有任何,Sleuth 只是覆盖了日志级别并将跟踪信息放入其中.如果您搜索文档中代码中提到的属性,您会发现 这正是发生的事情:
Based on this, you could think that there is nothing between the log level and the pid, Sleuth simply overrides the log level and places the tracing information into it. And if you search for the property that the docs mention in the code, you will found out that it is exactly what happens:
Sleuth 覆盖日志级别模式并将跟踪信息添加到其中:
Sleuth overrides the log level pattern and adds tracing info into it:
map.put("logging.pattern.level", "%5p [${spring.zipkin.service.name:" + "${spring.application.name:}},%X{traceId:-},%X{spanId:-}]");
这篇关于spring cloud sleuth 如何将跟踪信息添加到 logback 日志行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!