Docs » Collect application spans and traces » 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 🔗

The Splunk OTel Java agent automatically injects trace metadata into logs so that Java logging libraries can include tracing attributes in log statements. You can use trace metadata to correlate traces with log events and explore logs in Observability Cloud.

To include trace metadata in log statements you must configure one of the supported logging libraries.

Supported logging libraries 🔗

The Splunk OTel Java agent supports the following logging libraries:

  • Log4j 2 2.7 or higher

  • Log4j 1 1.2 or higher

  • Logback 1.0 or higher

Include injected trace data 🔗

The Splunk OTel Java agent injects the following attributes into logs:

  • Trace information: trace_id and span_id

  • Resource attributes: service.name and deployment.environment

The following example for Spring Boot configuration, which uses Logback, shows how you can include trace data in log statements produced by the logging library:

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 metadata 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 the logger libraries.

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

  • Log4j

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

    <pattern>service: %property{otel.resource.service.name}, env: %property{otel.resource.deployment.environment}: %m%n</pattern>
    

Exploring application logs 🔗

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 Splunk Log Observer.