<dependency> <groupId>org.apache.skywalking</groupId> <artifactId>apm-toolkit-log4j-2.x</artifactId> <version>{project.release.version}</version> </dependency>
[%traceId]
pattern in your log4j2.xml<Appenders> <Console name="Console" target="SYSTEM_OUT"> <PatternLayout pattern="%d [%traceId] %-5p %c{1}:%L - %m%n"/> </Console> </Appenders>
<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 , No additional configuration is required. Refer to the demo of log4j2.xml below.
For details: All Loggers Async
Log4j-2.9 and higher require disruptor-3.3.4.jar or higher on the classpath. Prior to Log4j-2.9, disruptor-3.0.0.jar or higher was required. This is simplest to configure and gives 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: Mixed Sync & Async
Log4j-2.9 and higher require disruptor-3.3.4.jar or higher on the classpath. Prior to Log4j-2.9, disruptor-3.0.0.jar or higher was required. There is no need to set 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: 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>
-javaagent
to active the sky-walking tracer, log4j2 will output traceId, if it existed. If the tracer is inactive, the output will be TID: N/A
.