Spring Cloud Sleuth如何将跟踪信息添加到Logback日志行 [英] How spring cloud sleuth adds tracing information to logback log lines

查看:62
本文介绍了Spring Cloud Sleuth如何将跟踪信息添加到Logback日志行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基于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 :

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 父级,它带来了 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)
根据我想出的问题,您已经弄清楚了 traceId spanId 已放入MDC.

如果您查看

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 属性来禁用它.

This log configuration was automatically setup by Sleuth. You can disable it by disabling Sleuth via spring.sleuth.enabled=false property or putting your own logging.pattern.level property.

仍然没有明确解释该机制,但是它给了您很大的提示:

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日志行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆