tencent cloud

All product documents
Tencent Cloud Observability Platform
Skywalking Protocol (Java)
Last updated: 2025-03-12 11:40:47
Skywalking Protocol (Java)
Last updated: 2025-03-12 11:40:47
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
Set %T in layout.ConversionPattern (in 2.0 - 2016, you should use %x, Why was it modified?).
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>
For details, see: [Mixed Sync & Async] #MixedSync-Async
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>
Support log4j2 AsyncAppender. For details, see Log4j2 AsyncAppender.
<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.



Was this page helpful?
You can also Contact Sales or Submit a Ticket for help.
Yes
No

Feedback

Contact Us

Contact our sales team or business advisors to help your business.

Technical Support

Open a ticket if you're looking for further assistance. Our Ticket is 7x24 avaliable.

7x24 Phone Support