Search code examples
spring-bootspring-cloudspring-cloud-sleuthspring-cloud-gcp

Configuring spring cloud sleuth and logback to log baggage fields


Following the spring cloud sleuth documentation, I've configured an app properties with the following:

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

I've then added a 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>

Yet the header is not logged when making requests


Solution

  • I've made a novice mistake and only needed to define the CONSOLE_LOG_PATTERN before including the defaults and other appenders.

    <?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>