Docs » Instrument back-end applications to send spans to Splunk APM » Instrument Java applications for Splunk Observability Cloud » Connect Java trace data with logs for Splunk Observability Cloud

Connect Java trace data with logs for Splunk Observability Cloud ๐Ÿ”—

You can configure Java logging libraries to include tracing attributes provided automatically by the Splunk OTel Java agent. Use the trace metadata to correlate traces with log events and explore logs in Splunk Observability Cloud.

Check compatibility and requirements ๐Ÿ”—

The Splunk OTel Java agent supports the following logging libraries:

  • Log4j 2 2.7 and higher

  • Log4j 1 1.2 and higher

  • Logback 1.0 and higher

Include trace metadata in log statements ๐Ÿ”—

The Splunk OTel Java agent provides the following attributes for logging libraries:

  • Trace information: trace_id and span_id

  • Resource attributes: service.name and deployment.environment

The following examples show how to include trace data in log statements produced by the logging library:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
   <Appenders>
      <Console name="STDOUT" target="SYSTEM_OUT">
         <JsonLayout compact="true" eventEol="true">
            <KeyValuePair key="trace_id" value="${ctx:trace_id}"/>
            <KeyValuePair key="span_id" value="${ctx:span_id}"/>
            <KeyValuePair key="service.name" value="${sys:otel.resource.service.name}"/>
            <KeyValuePair key="trace_sampled" value="${ctx:trace_flags}"/>
         </JsonLayout>
      </Console>
   </Appenders>
   <!-- More configuration -->
</Configuration>

Add resource attributes to your application logs ๐Ÿ”—

The Splunk Distribution of OpenTelemetry Java exposes resource attributes as system properties prefixed with otel.resource., which you can use to configure logger libraries.

The following examples show how to add Splunk OTel Java metadata to the logger configuration:

<PatternLayout>
   <pattern>
      service.name=${sys:otel.resource.service.name}, deployment.environment=${sys:otel.resource.deployment.environment} %m%n
   </pattern>
</PatternLayout>

If youโ€™re instrumenting a serverless service or application, use environment variables instead:

<PatternLayout>
   <pattern>
      service.name=${OTEL_SERVICE_NAME}, deployment.environment=${OTEL_ENV_NAME} %m%n
   </pattern>
</PatternLayout>

Explore application logs in Log Observer ๐Ÿ”—

You can send Java application logs to Splunk Observability Cloud in the same way you send any other type of log data. To learn more about logs in Observability Cloud, see Set up Log Observer.