Documentation forSolarWinds Observability SaaS

Java trace context in logs

When you add trace context to application logs, you can correlate the log messages from a single transaction, and if sampled, the log messages to the transaction trace detail.

The SolarWinds Observability Java Library uses the standard OpenTelemetry Java Library to inject trace context into logs managed by several popular Java logging frameworks.

Application logs for your service entity are not included in the data sent by APM libraries. Configure your server or application to send application logs to SolarWinds Observability SaaS. See Add logs from services.

If trace context is added to logs and those logs are sent to SolarWinds Observability SaaS, use the Traces Explorer to see the logs associated with a traced transaction.

Enable trace context

By default the trace_id, span_id, and trace_flags values are available as MDC data to configure into log messages. See Logger MDC auto-instrumentation in the OpenTelemetry Java Instrumentation documentation.

APM Java versions earlier than 1.2.0

To enable trace context in logs, configure the logging framework. For example, in Logback, the following configuration prepends the trace context before the log message:

<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender"> 
    <layout class="ch.qos.logback.classic.PatternLayout"> 
        <Pattern> 
            trace_id=%X{trace_id} span_id=%X{span_id} trace_flags=%X{trace_flags} resource.service.name=${service.name} %5p - %msg%n
        </Pattern> 
    </layout> 
</appender>

The MDC context for service name can be configured as follows:

  • Log4j2: resource.service.name=${sys:service.name}

  • Logback: resource.service.name=${service.name}

APM Java versions 1.2.0 and later

To make the Service Name available in MDC data, set the OTEL_INSTRUMENTATION_COMMON_MDC_RESOURCE_ATTRIBUTES environment variable or its system property equivalent otel.instrumentation.common.mdc.resource-attributes to the comma separated values of the resource attributes to be added to MDC. For example:

OTEL_INSTRUMENTATION_COMMON_MDC_RESOURCE_ATTRIBUTES=service.name

The MDC context for service name can then be configured as follows:

resource.service.name=%X{service.name}

To enable trace context in logs, configure the logging framework. For example, in Logback, the following configuration prepends the trace context before the log message:

<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender"> 
    <layout class="ch.qos.logback.classic.PatternLayout"> 
        <Pattern> 
            trace_id=%X{trace_id} span_id=%X{span_id} trace_flags=%X{trace_flags} resource.service.name=%X{service.name} %5p - %msg%n
        </Pattern> 
    </layout> 
</appender>

An example of the logs after the trace context being enabled is:

trace_id=87f03e188270c5b6d8bfffee45f78fcb span_id=b9c4101c7ec7dbdb trace_flags=01 resource.service.name=my-service INFO - hi this is an info log.

Supported frameworks

Log4j 1 1.2+
Log4j 2 2.7+
Logback 1.0+

The scripts are not supported under any SolarWinds support program or service. The scripts are provided AS IS without warranty of any kind. SolarWinds further disclaims all warranties including, without limitation, any implied warranties of merchantability or of fitness for a particular purpose. The risk arising out of the use or performance of the scripts and documentation stays with you. In no event shall SolarWinds or anyone else involved in the creation, production, or delivery of the scripts be liable for any damages whatsoever (including, without limitation, damages for loss of business profits, business interruption, loss of business information, or other pecuniary loss) arising out of the use of or inability to use the scripts or documentation.