Integrate the SLF4J MDC feature
SLF4J provides Mapped Diagnostic Contexts (MDC), a feature that lets you define and modify the format and content of your logs. This topic describes the SLF4J MDC feature that is integrated with Tracer. This lets you output the current TraceId and SpanId from the Tracer context by simply modifying the log configuration file.
Usage
Add the following Tracer dependencies to your project:
<dependency> <groupId>com.alipay.sofa</groupId> <artifactId>tracer-core</artifactId> </dependency> <dependency> <groupId>com.alipay.sofa</groupId> <artifactId>tracer-extensions</artifactId> </dependency>Modify the PatternLayout property in your existing log configuration file:
Logback example
<!-- This appender is an example for the Tracer MDC feature. Delete it for actual use. --> <appender name="MDC-EXAMPLE-APPENDER" class="ch.qos.logback.core.rolling.RollingFileAppender"> <append>true</append> <filter class="ch.qos.logback.classic.filter.LevelFilter"> <level>${logging.level}</level> <onMatch>ACCEPT</onMatch> <onMismatch>DENY</onMismatch> </filter> <file>${logging.path}/archtype-test-client/mdc-example.log</file> <!-- to generate a log file everyday with a longest lasting of 30 days --> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <!-- logfile name with daily rolling--> <FileNamePattern>${logging.path}/archtype-test-client/mdc-example.log.%d{yyyy-MM-dd}</FileNamePattern> <!-- log preserve days--> <MaxHistory>30</MaxHistory> </rollingPolicy> <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> <!--output format: %d is for date, %thread is for thread name, %-5level: loglevel with 5 characters, %msg: log message, %n: line breaker--> <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %X{SOFA-TraceId} %X{SOFA-SpanId} %logger{50} - %msg%n</pattern> <!-- encoding --> <charset>UTF-8</charset> </encoder> </appender>Log4j2 example
<?xml version="1.0" encoding="UTF-8"?> <configuration> <Appenders> <Console name="CONSOLE" target="SYSTEM_OUT"> <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} %5p [%X{SOFA-TraceId},%X{SOFA-SpanId}] ---- %m%n "/> </Console> <Filename="File"fileName="./logs/test.log"> <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} %5p [%X{SOFA-TraceId},%X{SOFA-SpanId}] ---- %m%n "/> </File> </Appenders> <Loggers> <root level="info"> <Appender Refref="CONSOLE"/> <Appender Refref="File"/> </root> </Loggers> </configuration>
Add the
%X{SOFA-TraceId}and%X{SOFA-SpanId}configurations to PatternLayout:%X{SOFA-TraceId}: Represents theTraceId. At runtime, this is replaced with the TraceId from the current Tracer context. If a Tracer context does not exist, it is replaced with an empty string.%X{SOFA-SpanId}: Represents theSpanId. At runtime, this is replaced with the SpanId from the current Tracer context. If a Tracer context does not exist, it is replaced with an empty string.
In your application code, use the following code to print logs. No other changes are required.
logger.info("rest mdc example");The log output is as follows:
2018-02-0515:52:45.037[SOFA-REST-WORK-3-1] INFO 0a0fe86f1517817164962100123030 MDC-EXAMPLE - rest mdc example