tlog实现链路追踪
TLog通过对日志打标签完成企业级微服务的日志追踪。它不收集日志,使用简单, 产生全局唯一追踪码。除了追踪码,TLog还支持SpanId和上下游服务信息 标签的追加。你还可以自定义方法级别的标签,让日志的定位轻而易举
集成springboot
依赖
<dependency>
<groupId>com.yomahub</groupId>
<artifactId>tlog-all-spring-boot-starter</artifactId>
<version>1.5.0</version>
</dependency>
控制层
package com.tlog.springboottlog.controller;
import com.tlog.springboottlog.config.TlogConvert;
import com.yomahub.tlog.core.annotation.TLogAspect;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
/**
* @author 蔡定努
* @date 2023/09/06 12:59
*/
@Slf4j
@RestController
public class TestController {
@GetMapping("request")
public void aa(HttpServletRequest request) {
log.info("--------完成--------------");
}
/**
* 默认方式
*
* @author 蔡定努
*/
@GetMapping("b")
public void b() {
log.info("默认方式");
}
/**
* 注解带参数
*
* @author 蔡定努
*/
@GetMapping("aspect")
@TLogAspect(value = {"id", "name"}, pattern = "<-{}->", joint = "_")
public void aspect(String id, String name) {
log.info("加了patter和joint的示例");
}
/**
* 注解,自己拼接链路
*
* @author 蔡定努
*/
@GetMapping("aspect2")
@TLogAspect(convert = TlogConvert.class)
public void aspect2(String id, String name) {
log.info("加了patter和joint的示例");
}
}
logback-spring.xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="false">
<property name="LOG_HOME" value="./logs" />
<springProperty scop="context" name="APP_NAME" source="spring.application.name" defaultValue=""/>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<!--这里替换成AspectLogbackEncoder-->
<encoder class="com.yomahub.tlog.core.enhance.logback.AspectLogbackEncoder">
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
</encoder>
</appender>
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<File>${LOG_HOME}/${APP_NAME}.log</File>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<FileNamePattern>${LOG_HOME}/${APP_NAME}.log.%d{yyyy-MM-dd}.%i.log</FileNamePattern>
<MaxHistory>30</MaxHistory>
<maxFileSize>1000MB</maxFileSize>
</rollingPolicy>
<!--这里替换成AspectLogbackEncoder-->
<encoder class="com.yomahub.tlog.core.enhance.logback.AspectLogbackEncoder">
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="STDOUT" />
<appender-ref ref="FILE" />
</root>
</configuration>
如果需要打印接口耗时,配置文件中添加一下配置
tlog.enable-invoke-time-print=on
效果: