Docs » Supported integrations in Splunk Observability Cloud » 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.

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:

Edit your Log4j configuration, for example in the src/main/resources/log4j2.xml file. Depending on your environment, you might have to edit a different file or use a different configuration system.

<?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 -->

For Spring Boot applications, you can also edit the file to add the following logging pattern:

logging.pattern.console = %d{yyyy-MM-dd HH:mm:ss} - %logger{36} - %msg trace_id=%X{trace_id} span_id=%X{span_id} trace_flags=%X{trace_flags} %n

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