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: 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">
      <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="" value="${}"/>
            <KeyValuePair key="trace_sampled" value="${ctx:trace_flags}"/>
   <!-- More 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:

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

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

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

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.