Skip to main content
Sumo Logic

TraceId and SpanId injection into logs configuration

It is very simple to configure traceId and spanId data injection into user logs in Java applications. In general, it is enough to add instrumented versions of the logging packages into project dependencies. Currently, Log4j2 and logback loggers are supported since OpenTelemetry-Java-Instrumentation version 0.10.1.

Log4j instrumentation

  1. Add instrumented Log4j2 library into the project dependencies:

  • Maven projects

    <dependencies>
      <dependency>
        <groupId>io.opentelemetry.instrumentation</groupId>
        <artifactId>opentelemetry-log4j-2.13.2</artifactId>
        <version>0.10.0</version>
        <scope>runtime</scope>
      </dependency>
    </dependencies>

  • Gradle projects

    dependencies {
      runtimeOnly("io.opentelemetry.instrumentation:opentelemetry-log4j-2.13.2:0.10.0")
    }

  1. Update the log4j2.xml configuration file, typically stored in resources directory, with traceId, spanId, and sampled keys. The following is an example configuration.

Logback instrumentation

  1. Add instrumented logback library into the project dependencies:

  • Maven projects

    <dependencies>
      <dependency>
        <groupId>io.opentelemetry.instrumentation</groupId>
        <artifactId>opentelemetry-logback-1.0.0</artifactId>
        <version>0.10.0</version>
        <scope>runtime</scope>
      </dependency>
    </dependencies>

  • Gradle projects

    dependencies {
      runtimeOnly("io.opentelemetry.instrumentation:opentelemetry-logback-1.0.0:0.10.0")
    }

  1. Update the logback.xml configuration file, typically stored in the resources directory, with traceId, spanId, and sampled keys. The following is an example configuration.

<?xml version="1.0" encoding="UTF-8" ?>
<configuration>
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern><![CDATA[%date{HH:mm:ss.SSS} [%thread] %-5level %logger{15}#%line %X{req.requestURI} traceId: %X{traceId} spanId: %X{spanId} %msg\n]]></pattern>
        </encoder>
    </appender>

    <appender name="OTEL" class="io.opentelemetry.instrumentation.logback.v1_0_0.OpenTelemetryAppender">
        <appender-ref ref="STDOUT" />
    </appender>

    <root>
        <level value="DEBUG" />
        <appender-ref ref="STDOUT" />
    </root>

</configuration>