1.引入skywalking的jar包,导入的包和agent版本一致
- <dependency>
- <groupId>org.apache.skywalking</groupId>
- <artifactId>apm-toolkit-trace</artifactId>
- <version>8.12.0</version>
- </dependency>
- <dependency>
- <groupId>org.apache.skywalking</groupId>
- <artifactId>apm-toolkit-opentracing</artifactId>
- <version>8.12.0</version>
- </dependency>
- <dependency>
- <groupId>org.apache.skywalking</groupId>
- <artifactId>apm-toolkit-logback-1.x</artifactId>
- <version>8.12.0</version>
- </dependency>
2.修改logback.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <configuration scan="true" scanPeriod="60 seconds" debug="false">
- <statusListener class="ch.qos.logback.core.status.NopStatusListener" />
- <!-- 日志存放路径 -->
- <property name="log.path" value="logs" />
-
- <!-- 日志输出格式 -->
- <property name="log.pattern"
- value="%d{HH:mm:ss.SSS} [%thread] %-5level %logger{20} %tid - [%method,%line] - %msg%n"/>:ss} %-5level
- ${springAppName:-} %thread %logger %msg%n"/>
-
- <!-- 控制台输出 -->
- <appender name="console" class="ch.qos.logback.core.ConsoleAppender">
- <encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
- <layout class="org.apache.skywalking.apm.toolkit.log.logback.v1.x.TraceIdPatternLogbackLayout">
- <pattern>${log.pattern}</pattern>
- </layout>
- </encoder>
- </appender>
-
- <!-- 系统日志输出 -->
- <appender name="file_info" class="ch.qos.logback.core.rolling.RollingFileAppender">
- <file>${log.path}/info.log</file>
- <!-- 循环政策:基于时间创建日志文件 -->
- <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
- <!-- 日志文件名格式 -->
- <fileNamePattern>${log.path}/info.%d{yyyy-MM-dd}-%i.log</fileNamePattern>
- <!-- 日志最大的历史 3天 -->
- <maxHistory>3</maxHistory>
- <!--单个日志文件的最大体积-->
- <maxFileSize>100MB</maxFileSize>
- <!--控制所有归档日志文件的总大小-->
- <totalSizeCap>200MB</totalSizeCap>
- <!--是否在应用启动的时候删除历史日志-->
- <cleanHistoryOnStart>true</cleanHistoryOnStart>
- </rollingPolicy>
- <encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
- <layout class="org.apache.skywalking.apm.toolkit.log.logback.v1.x.TraceIdPatternLogbackLayout">
- <pattern>${log.pattern}</pattern>
- </layout>
- </encoder>
- <filter class="ch.qos.logback.classic.filter.LevelFilter">
- <!-- 过滤的级别 -->
- <level>info</level>
- <!-- 匹配时的操作:接收(记录) -->
- <onMatch>ACCEPT</onMatch>
- <!-- 不匹配时的操作:拒绝(不记录) -->
- <onMismatch>DENY</onMismatch>
- </filter>
- </appender>
-
- <appender name="file_error" class="ch.qos.logback.core.rolling.RollingFileAppender">
- <file>${log.path}/error.log</file>
- <!-- 循环政策:基于时间创建日志文件 -->
- <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
- <!-- 日志文件名格式 -->
- <fileNamePattern>${log.path}/error.%d{yyyy-MM-dd}-%i.log</fileNamePattern>
- <!-- 日志最大的历史 1天 -->
- <maxHistory>3</maxHistory>
- <!--单个日志文件的最大体积-->
- <maxFileSize>100MB</maxFileSize>
- <!--控制所有归档日志文件的总大小-->
- <totalSizeCap>200MB</totalSizeCap>
- <!--是否在应用启动的时候删除历史日志-->
- <cleanHistoryOnStart>true</cleanHistoryOnStart>
- </rollingPolicy>
- <encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
- <layout class="org.apache.skywalking.apm.toolkit.log.logback.v1.x.TraceIdPatternLogbackLayout">
- <pattern>${log.pattern}</pattern>
- </layout>
- </encoder>
- <filter class="ch.qos.logback.classic.filter.LevelFilter">
- <!-- 过滤的级别 -->
- <level>error</level>
- <!-- 匹配时的操作:接收(记录) -->
- <onMatch>ACCEPT</onMatch>
- <!-- 不匹配时的操作:拒绝(不记录) -->
- <onMismatch>DENY</onMismatch>
- </filter>
- </appender>
-
- <!-- 日志传输到skywalking中的appender,通过qrpc传输 -->
- <appender name="gpc-log" class="org.apache.skywalking.apm.toolkit.log.logback.v1.x.log.GRPCLogClientAppender">
- <encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
- <layout class="org.apache.skywalking.apm.toolkit.log.logback.v1.x.mdc.TraceIdMDCPatternLogbackLayout">
- <Pattern>${log.pattern}</Pattern>
- </layout>
- </encoder>
- </appender>
-
- <!--系统操作日志-->
- <root level="info">
- <appender-ref ref="file_info"/>
- <appender-ref ref="file_error"/>
- <appender-ref ref="console"/>
- <appender-ref ref="gpc-log" />
- </root>
-
- </configuration>
看到日志打印出TID则表示配置成功
3.自定义标签快速定位异常
- private final Tracer tracer = new SkywalkingTracer();
-
- private void setSkywalkingTagErrMsg(String msg, String code) {
- ActiveSpan span = tracer.activeSpan();
- span.setTag("error.msg", msg);
- span.setTag("error.code", code);
- }