This module integrates instrumentation with Logback by injecting the trace ID and span ID from a mounted span using a custom Logback appender.
Replace OPENTELEMETRY_VERSION
with the latest
release.
For Maven, add to your pom.xml
dependencies:
<dependencies>
<dependency>
<groupId>io.opentelemetry.instrumentation</groupId>
<artifactId>opentelemetry-logback-mdc-1.0</artifactId>
<version>OPENTELEMETRY_VERSION</version>
</dependency>
</dependencies>
For Gradle, add to your dependencies:
dependencies {
runtimeOnly("io.opentelemetry.instrumentation:opentelemetry-logback-mdc-1.0:OPENTELEMETRY_VERSION")
}
The following demonstrates how you might configure the appender in your logback.xml
configuration:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} trace_id=%X{trace_id} span_id=%X{span_id} trace_flags=%X{trace_flags} %msg%n</pattern>
</encoder>
</appender>
<!-- Just wrap your logging appender, for example ConsoleAppender, with OpenTelemetryAppender -->
<appender name="OTEL" class="io.opentelemetry.instrumentation.logback.mdc.v1_0.OpenTelemetryAppender">
<appender-ref ref="CONSOLE"/>
</appender>
<!-- Use the wrapped "OTEL" appender instead of the original "CONSOLE" one -->
<root level="INFO">
<appender-ref ref="OTEL"/>
</root>
</configuration>
It's important to note you can also use other encoders in the
ConsoleAppender
like logstash-logback-encoder. This can be helpful when theSpan
is invalid and thetrace_id
,span_id
, andtrace_flags
are allnull
and are hidden entirely from the logs.
Logging events will automatically have context information from the span context injected. The following attributes are available for use:
trace_id
span_id
trace_flags
These keys can be customized in your logback.xml
configuration, for example:
<appender name="OTEL" class="io.opentelemetry.instrumentation.logback.mdc.v1_0.OpenTelemetryAppender">
<traceIdKey>example_trace_id</traceIdKey>
<spanIdKey>example_span_id</spanIdKey>
<traceFlagsKey>example_trace_flags</traceFlagsKey>
</appender>
If you set <addBaggage>true</addBaggage>
in your logback.xml
configuration,
key/value pairs in baggage will also be added to the MDC.
baggage.<entry_name>