• Dependency the toolkit, such as using maven or gradle
   <dependency>
      <groupId>org.apache.skywalking</groupId>
      <artifactId>apm-toolkit-log4j-1.x</artifactId>
      <version>{project.release.version}</version>
   </dependency>

Print trace ID in your logs

  • Config a layout
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 change? )
log4j.appender.CONSOLE.layout.ConversionPattern=%d [%T] %-5p %c{1}:%L - %m%n
  • When you use -javaagent to active the SkyWalking tracer, log4j will output traceId, if it existed. If the tracer is inactive, the output will be TID: N/A.

Print SkyWalking context in your logs

  • Your only need to replace pattern %T with %T{SW_CTX}.

  • When you use -javaagent to active the SkyWalking tracer, log4j will output SW_CTX: [$serviceName,$instanceName,$traceId,$traceSegmentId,$spanId], if it existed. If the tracer is inactive, the output will be SW_CTX: N/A.

gRPC reporter

The gRPC report could forward the collected logs to SkyWalking OAP server, or SkyWalking Satellite sidecar. Trace id, segment id, and span id will attach to logs automatically. You don’t need to change the layout.

  • Add GRPCLogClientAppender in log4j.properties
log4j.rootLogger=INFO,CustomAppender
log4j.appender.CustomAppender=org.apache.skywalking.apm.toolkit.log.log4j.v1.x.log.GRPCLogClientAppender
log4j.appender.CustomAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.CustomAppender.layout.ConversionPattern=[%t] %-5p %c %x - %m%n
  • Add config of the plugin or use default
log.max_message_size=${SW_GRPC_LOG_MAX_MESSAGE_SIZE:10485760}