带有SpringBoot的Spring Sleuth(对数相关)-Traceid&SpanId不显示 [英] Spring sleuth with SpringBoot (log correlation) - Traceid & SpanId Not displayed

查看:173
本文介绍了带有SpringBoot的Spring Sleuth(对数相关)-Traceid&SpanId不显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个现有的Spring boot(2.2.0)应用程序,并试图集成Spring cloud sleuth以获取自动跟踪和跨度ID.登录文件如下-

 < configuration><属性名称="LOGS" value ="./logs"/>< appender name =控制台"class ="ch.qos.logback.core.ConsoleAppender">< layout class ="ch.qos.logback.classic.PatternLayout"><模式>%black(%d {ISO8601})%highlight(%-5level)[%blue(%t)]%yellow(%C {1.}):%msg%n%可抛出</模式></layout></appender>< appender name ="RollingFile"class ="ch.qos.logback.core.rolling.RollingFileAppender">< file> $ {LOGS}/spring-boot-logger.log</file><编码器class ="ch.qos.logback.classic.encoder.PatternLayoutEncoder"><模式>%d%p%C {1.} [%t]%m%n<</encoder>< rollingPolicyclass ="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"><!-每天进行翻转,并且文件达到100 MB时->< fileNamePattern> $ {LOGS}/archived/spring-boot-logger-%d {yyyy-MM-dd}.%i.log</fileNamePattern>< timeBasedFileNamingAndTriggeringPolicyclass ="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">< maxFileSize> 100MB</maxFileSize></timeBasedFileNamingAndTriggeringPolicy></rollingPolicy></appender><!-以指定级别记录所有内容->< root level ="info"><appender-ref ref="RollingFile"/>< appender-ref ref =控制台"/></root> 

此用于登录的配置未按预期方式记录/显示traceId.
据我所知,除了更新pom.xml如下所示,不需要做其他任何事情-

 < dependencyManagement><依赖关系><依赖关系>< groupId> org.springframework.cloud</groupId>< artifactId> spring-cloud-sleuth</artifactId>< version> 2.2.0.RELEASE</version>< type> pom</type>< scope> import</scope></dependency></dependencies></dependencyManagement> 

 < dependency>< groupId> org.springframework.cloud</groupId>< artifactId> spring-cloud-starter-sleuth</artifactId></dependency> 

令人惊讶的是,如果我在logback配置中包含以下内容-

 < include resource ="org/springframework/boot/logging/logback/base.xml"/> 

traceid,spanid和应用程序名称显示在console上.可能来自base.xml配置.

您知道我的登录文件或任何其他配置可能出什么问题吗?配置中缺少任何内容吗?

感谢您的帮助.

解决方案

好的侦探通过覆盖/扩展日志模式的日志级别字段"(在 org.springframework内部),将traceId和spanId写入日志.cloud.sleuth.autoconfig.TraceEnvironmentPostProcessor )

因此在 defaults.xml 中(导入到 base.xml 中)春天将模式定义为:

 <属性名称="CONSOLE_LOG_PATTERN" value ="$ {CONSOLE_LOG_PATTERN:-%clr(%d {$ {LOG_DATEFORMAT_PATTERN:-yyyy-MM-dd HH:mm:ss.SSS}}){淡}%clr($ {LOG_LEVEL_PATTERN:-%5p})%clr($ {PID:-}){洋红色}%clr(-){faint}%clr([%15.15t]){faint}%clr(%-40.40logger {39}){cyan}%clr(:){faint}%m%n $ {LOG_EXCEPTION_CONVERSION_WORD:-%wEx}}}"/> 

如您所见,是否定义了变量 LOG_LEVEL_PATTERN ,它是一个变量(然后在 TraceEnvironmentPostProcessor 中覆盖/扩展,如果未定义,则默认为默认级别模式%5p

因此,如果您包括base.xml,那么侦探将无法将日志级别的模式调整"为 LOG_LEVEL_PATTERN ,那么该模式中就不存在

根据文档说明( https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#howto-logging ),您总是应该在您的自定义logback.xml中包含base.xml,并且您很好.

不推荐:
尝试将日志级别模式定义为 $ {LOG_LEVEL_PATTERN:-%5p} ,而不只是%5p %-5level (但解决方案为如果您要问我,包括base.xml是正确的解决方案.

还:
https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-custom-log-configuration 列出了application.(properties | yml)字段,用于从属性中调整spring-boot日志.

例如我有自定义 BaggageFields 用于向下游传播,我有application.properties以在日志文件中看到它们,我按如下方式定义应用程序属性:(我定义自定义logback.xml!)

侦探2.0 +

 记录:图案:级别:%5p [$ {spring.zipkin.service.name:$ {spring.application.name:-}},%X{X-B3-TraceId:-},%X{X-B3-SpanId:-},%X {X-Span-Export:-}] [%X {ddd:-},%X {bp:-},%X {bpids:-}] [%X {op:-},%X{chunk:-},%X {i:-}]" 

侦探3.0 +

 记录:图案:级别:%5p [$ {spring.zipkin.service.name:$ {spring.application.name:}},%X{traceId:-},%X{spanId:-}] [%X {ddd:-},%X{bp:-},%X{bpids:-}] [%X{op:-},%X{chunk:-},%X{i:-}]" 

I have an existing Spring boot (2.2.0) application and trying to integrate Spring cloud sleuth to get automatic trace and span id. The logback file is as follows -

<configuration>

<property name="LOGS" value="./logs" />

<appender name="Console"
          class="ch.qos.logback.core.ConsoleAppender">
    <layout class="ch.qos.logback.classic.PatternLayout">
        <Pattern>
            %black(%d{ISO8601}) %highlight(%-5level) [%blue(%t)] %yellow(%C{1.}): %msg%n%throwable
        </Pattern>
    </layout>
</appender>

<appender name="RollingFile"
          class="ch.qos.logback.core.rolling.RollingFileAppender">
    <file>${LOGS}/spring-boot-logger.log</file>
    <encoder
            class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
        <Pattern>%d %p %C{1.} [%t] %m%n</Pattern>
    </encoder>

    <rollingPolicy
            class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
        <!-- rollover daily and when the file reaches 100 MegaBytes -->
        <fileNamePattern>${LOGS}/archived/spring-boot-logger-%d{yyyy-MM-dd}.%i.log
        </fileNamePattern>
        <timeBasedFileNamingAndTriggeringPolicy
                class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
            <maxFileSize>100MB</maxFileSize>
        </timeBasedFileNamingAndTriggeringPolicy>
    </rollingPolicy>
</appender>

<!-- LOG everything at specified level level -->
<root level="info">
    <appender-ref ref="RollingFile" />
    <appender-ref ref="Console" />
</root>

This config for logback does not log/display traceId as expected.
As far as i know, nothing else is required except update to pom.xml which is as follows -

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-sleuth</artifactId>
            <version>2.2.0.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

and

<dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-sleuth</artifactId>
    </dependency>

Surprisingly enough, if I include following in the logback config -

<include resource="org/springframework/boot/logging/logback/base.xml"/>

The traceid, spanid and application name is displayed on console . Probably from the base.xml config.

Any idea what might be wrong with my logback file or any other config? Is there anything missing in the config?

Any help is appreciated.

解决方案

Well sleuth gets traceId and spanId into logs by overwriting/extending the log-level "field" of the log-pattern (inside org.springframework.cloud.sleuth.autoconfig.TraceEnvironmentPostProcessor)

so in defaults.xml (imported in the base.xml) spring defines the pattern to be:

    <property name="CONSOLE_LOG_PATTERN" value="${CONSOLE_LOG_PATTERN:-%clr(%d{${LOG_DATEFORMAT_PATTERN:-yyyy-MM-dd HH:mm:ss.SSS}}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}}"/>

as you can see if variable LOG_LEVEL_PATTERN is defined it's the one taken (and then overwritten/extended in TraceEnvironmentPostProcessor, if not, it defaults to the default level pattern %5p

so if you do not include base.xml sleuth will not be able to "adjust" the log-level pattern as LOG_LEVEL_PATTERN then just does not exist in the pattern.

As documentation states (https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#howto-logging) you always should include base.xml in your custom logback.xml, and you're fine.

not recommended:
try to define your log level pattern as ${LOG_LEVEL_PATTERN:-%5p} instead of just %5p or %-5level (but solution of including base.xml is the correct solution, if you'd ask me.

also:
https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-custom-log-configuration lists application.(properties|yml) fields to tweak spring-boot logging from properties.

e.g. I have custom BaggageFields for propagating downstream and my application.properties to see them in logfiles I define application properties as follows: (I do not define a custom logback.xml!)

for sleuth 2.0+

logging:
  pattern:
    level: "%5p [${spring.zipkin.service.name:${spring.application.name:-}},%X{X-B3-TraceId:-},%X{X-B3-SpanId:-},%X{X-Span-Export:-}] [%X{ddd:-},%X{bp:-},%X{bpids:-}] [%X{op:-},%X{chunk:-},%X{i:-}]"

for sleuth 3.0+

logging:
  pattern:
    level: "%5p [${spring.zipkin.service.name:${spring.application.name:}},%X{traceId:-},%X{spanId:-}] [%X{ddd:-},%X{bp:-},%X{bpids:-}] [%X{op:-},%X{chunk:-},%X{i:-}]"

这篇关于带有SpringBoot的Spring Sleuth(对数相关)-Traceid&amp;SpanId不显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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