Documentation forSolarWinds Observability

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.

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=%mdc{trace_id} span_id=%mdc{span_id} trace_flags=%mdc{trace_flags} %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  INFO -  hi this is a info log.

See Logger MDC auto-instrumentation in the OpenTelemetry Java Instrumentation documentation.

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.