In a Spring Boot app with OpenTelemetry, how can I add an OpenTelemetryAppender for logs without changing the console logging?

219 views Asked by At

In a spring boot app, with Open Telemetry, when you have no logback.xml or logback-spring.xml there are console logs that include the traceId and spanId values.

I need to add an OpenTelemetryAppender, and thus I have to add a configuration file. This means I have to specify both OpenTelemetryAppender and ConsoleAppender, but ConsoleAppender needs an encoder and a pattern.

I don't want to change the pattern, I want to keep the existing pattern.

How could I do this?

Every attempt to specify no pattern fails with errors. Every example pattern I have found searching leaves out traceId and spanId.

2

There are 2 answers

5
Gregor Zeitlinger On

You can add the appender programatically, like here.

You can also just use a starter that adds the appender for you:

0
John Windberg On

I found that this works just fine:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <include resource="org/springframework/boot/logging/logback/defaults.xml"/>
    <include resource="org/springframework/boot/logging/logback/console-appender.xml"/>
    <appender name="OpenTelemetry"
              class="io.opentelemetry.instrumentation.logback.appender.v1_0.OpenTelemetryAppender">
        <captureExperimentalAttributes>true</captureExperimentalAttributes>
        <captureKeyValuePairAttributes>true</captureKeyValuePairAttributes>
    </appender>
    <root level="INFO">
        <appender-ref ref="CONSOLE"/>
        <appender-ref ref="OpenTelemetry"/>
    </root>
</configuration>