配置 spring 云侦探和 logback 以记录行李字段

Configuring spring cloud sleuth and logback to log baggage fields

spring cloud sleuth documentation 之后,我使用以下内容配置了一个应用程序属性:

spring.sleuth.baggage.remote-fields=x-header-to-log
spring.sleuth.baggage.correlation-fields=x-header-to-log

然后我添加了一个 logback-spring。xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <include resource="org/springframework/boot/logging/logback/defaults.xml"/>

  <springProperty scope="context" name="springAppName" source="spring.application.name"/>

  <!-- You can override this to have a custom pattern -->
  <property name="CONSOLE_LOG_PATTERN"
      value="%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %X{x-header-to-log:-} %clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}"/>

  <!-- Appender to log to console -->
  <appender name="console" class="ch.qos.logback.core.ConsoleAppender">
    <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
      <!-- Minimum logging level to be presented in the console logs-->
      <level>DEBUG</level>
    </filter>
    <encoder>
      <pattern>${CONSOLE_LOG_PATTERN}</pattern>
      <charset>utf8</charset>
    </encoder>
  </appender>

  <root level="INFO">
    <appender-ref ref="console"/>
  </root>
</configuration>

但是在发出请求时 header 没有被记录

我犯了一个新手错误,只需要在包含默认值和其他附加程序之前定义 CONSOLE_LOG_PATTERN

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <springProperty scope="context" name="springAppName" source="spring.application.name"/>

  <!-- You can override this to have a custom pattern -->
  <property name="CONSOLE_LOG_PATTERN"
      value="%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(%X{x-header-to-log:-}){cyan} %clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}"/>

  <include resource="org/springframework/boot/logging/logback/defaults.xml"/>
  <include resource="org/springframework/boot/logging/logback/console-appender.xml" />

  <appender name="SENTRY" class="io.sentry.logback.SentryAppender" />

  <root level="INFO">
    <appender-ref ref="CONSOLE"/>
    <appender-ref ref="SENTRY" />
  </root>
</configuration>