This article will introduce how Java SkyWalking reporting method associates with TraceID.
Directions
Configure Logback To Output TraceID
1. Add dependencies.
<dependency>
<groupId>org.apache.skywalking</groupId>
<artifactId>apm-toolkit-logback-1.x</artifactId>
<version>8.3.0</version>
</dependency>
2. Modify the Pattern format of Appender in logback-spring.xml.
<encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
<layout class="org.apache.skywalking.apm.toolkit.log.logback.v1.x.TraceIdPatternLogbackLayout">
<Pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%tid] [%thread] %-5level %logger{36} -%msg%n</Pattern>
</layout>
</encoder>
3. Launch the project, and the output is as follows:
%tid will print TraceID. The default TID is N/A. When there is a request call, TraceID will be displayed.
Configure Log4j-1x To Output TraceID
1. Introduce toolkit dependencies through Maven or Gradle.
<dependency>
<groupId>org.apache.skywalking</groupId>
<artifactId>apm-toolkit-log4j-1.x</artifactId>
<version>{project.release.version}</version>
</dependency>
2. Modify the layout configuration in log4j1.properties.
log4j.appender.CONSOLE.layout=org.apache.skywalking.apm.toolkit.log.log4j.v1.x.TraceIdPatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%d [%T] %-5p %c{1}:%L - %m%n
3. Start the project, and the printout is as follows:
Assuming that TraceID exists, when you activate the SkyWalking tracer with -javaagent
, log4j will output TraceID. If the tracer is not activated, the output will be TID: N/A
.
Configure Log4j-2x To Output TraceID
1. Use Maven or Gradle to introduce toolkit dependencies.
<dependency>
<groupId>org.apache.skywalking</groupId>
<artifactId>apm-toolkit-log4j-2.x</artifactId>
<version>{project.release.version}</version>
</dependency>
2. Configure [%traceId]
in the pattern of log4j2.xml.
Support configuring [%traceId] in the pattern of log4j2.xml.
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d [%traceId] %-5p %c{1}:%L - %m%n"/>
</Console>
</Appenders>
Support log4j2 AsyncRoot without other configurations. See the following log4j2.xml demonstration. For details, see Log4j2 Asynchronous Logger. <Configuration>
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d [%traceId] %-5p %c{1}:%L - %m%n"/>
</Console>
</Appenders>
<Loggers>
<AsyncRoot level="INFO">
<AppenderRef ref="Console"/>
</AsyncRoot>
</Loggers>
</Configuration>
Support log4j2 AsyncAppender without other configurations. See the following log4j2.xml demonstration. For details, see All Loggers Async. Note:
Log4j-2.9 and later versions require disruptor-3.3.4.jar or later on the classpath. Before Log4j-2.9, interrupter-3.0.0.jar or later is needed. This is the simplest configuration and offers the best performance. To make all loggers asynchronous, add the disruptor jar to the classpath and set the system property log4j2.contextSelector
to org.apache.logging.log4j.core.async.AsyncLoggerContextSelector
.
<Configuration status="WARN">
<Appenders>
<!-- Async Loggers will auto-flush in batches, so switch off immediateFlush. -->
<RandomAccessFile name="RandomAccessFile" fileName="async.log" immediateFlush="false" append="false">
<PatternLayout>
<Pattern>%d %p %c{1.} [%t] [%traceId] %m %ex%n</Pattern>
</PatternLayout>
</RandomAccessFile>
</Appenders>
<Loggers>
<Root level="info" includeLocation="false">
<AppenderRef ref="RandomAccessFile"/>
</Root>
</Loggers>
</Configuration>
Note:
Log4j-2.9 and later versions require disruptor-3.3.4.jar or later on the classpath. Before Log4j-2.9, disruptor-3.0.0.jar or later is needed. There is no need to set the system property “Log4jContextSelector” to any value.
<Configuration status="WARN">
<Appenders>
<!-- Async Loggers will auto-flush in batches, so switch off immediateFlush. -->
<RandomAccessFile name="RandomAccessFile" fileName="asyncWithLocation.log"
immediateFlush="false" append="false">
<PatternLayout>
<Pattern>%d %p %class{1.} [%t] [%traceId] %location %m %ex%n</Pattern>
</PatternLayout>
</RandomAccessFile>
</Appenders>
<Loggers>
<!-- pattern layout actually uses location, so we need to include it -->
<AsyncLogger name="com.foo.Bar" level="trace" includeLocation="true">
<AppenderRef ref="RandomAccessFile"/>
</AsyncLogger>
<Root level="info" includeLocation="true">
<AppenderRef ref="RandomAccessFile"/>
</Root>
</Loggers>
</Configuration>
<Configuration>
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d [%traceId] %-5p %c{1}:%L - %m%n"/>
</Console>
<Async name="Async">
<AppenderRef ref="Console"/>
</Async>
</Appenders>
<Loggers>
<Root level="INFO">
<AppenderRef ref="Async"/>
</Root>
</Loggers>
</Configuration>
3. Launch the project, and the output is as follows.
Assuming that TraceID exists, when you activate the SkyWalking tracer with -javaagent
, log4j will output TraceID. If the tracer is not activated, the output will be TID: N/A
.