一、sleuth介绍
sleuth 是一个分布式跟踪系统,用于跟踪应用程序中的请求和操作。引入该依赖后,日志中会自动添加(traceid,spanid),但是sleuth也可以在SpringBoot中单独使用
二、sleuth引用
#pom.xml引入spring-cloud
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>2021.0.5</version>
<type>pom</type>
<scope>import</scope>
</dependency>
#业务的pom.xml中引入sleuth包
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-sleuth</artifactId>
</dependency>
三、日志文件的修改
<?xml version="1.0" encoding="UTF-8"?>
<!-- 从高到地低 OFF、FATAL、ERROR、WARN、INFO、DEBUG、TRACE、ALL -->
<!-- 日志输出规则 根据当前ROOT 级别,日志输出时,级别高于root默认的级别时会输出 -->
<!-- 以下每个配置的 filter 是过滤掉输出文件里面,会出现高级别文件,依然出现低级别的日志信息,通过 filter 过滤只记录本级别的日志 -->
<!-- 属性描述 scan:性设置为true时,配置文件如果发生改变,将会被重新加载,默认值为true scanPeriod:设置监测配置文件是否有修改的时间间隔,如果没有给出时间单位,默认单位是毫秒。当scan为true时,此属性生效。默认的时间间隔为1分钟。
debug:当此属性设置为true时,将打印出logback内部日志信息,实时查看logback运行状态。默认值为false。 -->
<configuration scan="true" scanPeriod="60 seconds"
debug="false">
<!-- 日志最大的历史 30天 -->
<property name="maxHistory" value="60" />
<!-- 日志最大的文件大小 300MB -->
<property name="maxFileSize" value="100MB" />
<property name="BaseLogName" value="order" />
<property name="logPath" value="d:/test/log" />
<property name="FILE_LOG_PATTERN" value="%d{yyyy-MM-dd HH:mm:ss.SSS} [%X{tid}] [%X{traceId:-},%X{spanId:-}] [%thread] %-5level %logger{50} - %msg%n"/>
<!-- 控制台输出 -->
<appender name="STDOUT"
class="ch.qos.logback.core.ConsoleAppender">
<encoder
class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%n是换行符 -->
<!--
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
-->
<pattern>${FILE_LOG_PATTERN}</pattern>
</encoder>
</appender>
<appender name="DEBUG"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${logPath}/debug.log</file>
<rollingPolicy
class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>
${logPath}/%d{yyyy-MM-dd}/debug.%d{yyyy-MM-dd}.%i.log.gz
</fileNamePattern>
<maxHistory>${maxHistory}</maxHistory>
<timeBasedFileNamingAndTriggeringPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>${maxFileSize}</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
<encoder>
<!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%n是换行符 -->
<!--
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50}-%msg%n</pattern>
-->
<pattern>${FILE_LOG_PATTERN}</pattern>
</encoder>
</appender>
<!--myibatis log configure -->
<logger name="com.apache.ibatis" level="ERROR" />
<logger name="com.zaxxer.hikari" level="WARN" />
<logger name="io.lettuce.core" level="WARN" />
<logger name="org.hibernate.validator" level="ERROR" />
<logger name="org.springframework" level="WARN" />
<logger name="org.mybatis.spring" level="WARN" />
<logger name="java.sql.Connection" level="ERROR" />
<logger name="java.sql.Statement" level="WARN" />
<logger name="java.sql.PreparedStatement" level="DEBUG" />
<!-- 日志输出级别 -->
<root level="DEBUG">
<appender-ref ref="STDOUT" />
<appender-ref ref="DEBUG" />
</root>
</configuration>
四、application.yml的修改
spring:
sleuth:
enabled: true
五、启动Application应用查看traceId信息
2023-11-26 12:47:40.056 [TID:cac59d5c559740d8bc10443e9aafe964.129.17009740600380001] [c118555cfa8cf4f8,c118555cfa8cf4f8] [http-nio-8081-exec-1] INFO o.a.c.c.C.[.[.[/demobigdata] -Initializing Spring DispatcherServlet 'dispatcherServlet'
2023-11-26 12:47:40.129 [TID:cac59d5c559740d8bc10443e9aafe964.129.17009740600380001] [c118555cfa8cf4f8,c118555cfa8cf4f8] [http-nio-8081-exec-1] INFO o.d.w.c.a.c.AccountController -ent getAllAccount
2023-11-26 12:47:40.129 [TID:cac59d5c559740d8bc10443e9aafe964.129.17009740600380001] [c118555cfa8cf4f8,c118555cfa8cf4f8] [http-nio-8081-exec-1] INFO o.d.s.a.impl.AccountServiceImpl -queryAllAccount
2023-11-26 12:47:40.199 [TID:cac59d5c559740d8bc10443e9aafe964.129.17009740600380001] [c118555cfa8cf4f8,c118555cfa8cf4f8] [http-nio-8081-exec-1] DEBUG com.clickhouse.jdbc.ClickHouseDriver -ClickHouse Driver 0.5.0.0(JDBC: 4.2.0.0) registered
2023-11-26 12:47:40.446 [TID:cac59d5c559740d8bc10443e9aafe964.129.17009740600380001] [c118555cfa8cf4f8,c118555cfa8cf4f8] [http-nio-8081-exec-1] INFO c.alibaba.druid.pool.DruidDataSource -{dataSource-1} inited
2023-11-26 12:47:40.651 [TID:cac59d5c559740d8bc10443e9aafe964.129.17009740600380001] [c118555cfa8cf4f8,c118555cfa8cf4f8] [http-nio-8081-exec-1] DEBUG o.d.m.d.a.A.selectAllAccount -==> Preparing: select id,username,password from account
2023-11-26 12:47:40.670 [TID:cac59d5c559740d8bc10443e9aafe964.129.17009740600380001] [c118555cfa8cf4f8,c118555cfa8cf4f8] [http-nio-8081-exec-1] DEBUG o.d.m.d.a.A.selectAllAccount -==> Parameters:
2023-11-26 12:47:40.697 [TID:cac59d5c559740d8bc10443e9aafe964.129.17009740600380001] [c118555cfa8cf4f8,c118555cfa8cf4f8] [http-nio-8081-exec-1] DEBUG o.d.m.d.a.A.selectAllAccount -<== Total: 91
这样springboot就成功引入sleuth入了。