1、Add dependence on pom.xml
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.9</version>
</dependency>
<dependency>
<groupId>net.logstash.logback</groupId>
<artifactId>logstash-logback-encoder</artifactId>
<version>6.6</version>
</dependency>
2、Add or modify the logback.xml of your service’s code.
<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="true" scan="true" scanPeriod="1 seconds">
<contextName>logback</contextName>
<!--ConsoleAppender Used to output logs on the screen-->
<appender name="console_out" class="ch.qos.logback.core.ConsoleAppender">
<!--A filter is defined, the log output under LEVEL will not be printed-->
<!--INFO is defined here, that is, the console will not output
a log smaller than the INFO level-->
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>INFO</level>
</filter>
<!--Define the console output format-->
<encoder class="net.logstash.logback.encoder.LoggingEventCompositeJsonEncoder">
<!--The following is a new configuration
to solve Chinese garbled characters, \u2028 is the line separator-->
<jsonFactoryDecorator class="net.logstash.logback.decorate.CharacterEscapesJsonFactoryDecorator">
<escape>
<targetCharacterCode>10</targetCharacterCode>
<escapeSequence>\u2028</escapeSequence>
</escape>
</jsonFactoryDecorator>
<providers>
<pattern>
<pattern>
{"dd": {"trace_id": "%X{dd.trace_id}",
"span_id": "%X{dd.span_id}"
},
"level":"%-5level",
"message":"[%date{yyyy-MM-dd HH:mm:ss.SSS}] [%thread] %-5level env:${dd.env} service:${dd.service} class:%logger{80}:%line - %message %exception{full}"
}
</pattern>
</pattern>
</providers>
</encoder>
</appender>
<!-- ASYNC OUTPUT -->
<appender name="console_async" class="ch.qos.logback.classic.AsyncAppender">
<!-- Do not lose logs. By default, if 80% of the
queue is full, logs of TRACT, DEBUG, and INFO levels will be discarded -->
<discardingThreshold>0</discardingThreshold>
<!-- Change the default queue depth, this value will affect performance. The default value is 256 -->
<queueSize>2048</queueSize>
<!-- Print stack information -->
<includeCallerData>true</includeCallerData>
<appender-ref ref="console_out"/>
</appender>
<root level="INFO">
<appender-ref ref="console_async"/>
<!--<appender-ref ref="file_async"/>-->
</root>
</configuration>
3、re-deploy the api’s service