一、 为什么要做日志链路追踪
- 日志链路追踪(Log Path Tracing)是Spring Boot项目的一项关键功能,它通过将日志消息的源头与其对应的请求或响应路径相关联,实现对日志数据的可视化跟踪。
- 随着项目规模的扩大和复杂性的增加,追踪和管理日志数据变得越来越重要。通过实现日志链路追踪,我们可以更好地理解项目中发生的各种事件,识别瓶颈并快速定位问题。这不仅可以提高开发效率,还能保证项目质量。
二、实现方法
- 本文使用 MDC 方案实现日志链路追踪
- DC(Mapped Diagnostic Context,映射调试上下文)是 log4j 和 logback 提供的一种方便在多线程条件下记录日志的功能。
- 效果展示:
三、 实现步骤
- 准备依赖 (可根据自己的项目修改springboot 的版本)
<dependency>
<groupId>org.apache.skywalking</groupId>
<artifactId>apm-toolkit-logback-1.x</artifactId>
<version>8.15.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.7.10</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-actuator-autoconfigure</artifactId>
</dependency>
- 配置logback日志打印
- 在项目resources目录下创建logback-spring.xml
- 配置如下
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<contextName>${APP_NAME}</contextName>
<springProperty name="APP_NAME" scope="context" source="spring.application.name"/>
<springProperty name="LOG_FILE" scope="context" source="logging.file" defaultValue="../logs/application/${APP_NAME}"/>
<springProperty name="LOG_MAXFILESIZE" scope="context" source="logback.filesize" defaultValue="50MB"/>
<springProperty name="LOG_FILEMAXDAY" scope="context" source="logback.filemaxday" defaultValue="30"/>
<springProperty name="ServerIP" scope="context" source="spring.cloud.client.ip-address" defaultValue="0.0.0.0"/>
<springProperty name="ServerPort" scope="context" source="server.port" defaultValue="0000"/>
<springProperty name="LOG_POINT_FILE" scope="context" source="logging.file" defaultValue="../logs/point"/>
<springProperty name="LOG_AUDIT_FILE" scope="context" source="logging.file" defaultValue="../logs/audit"/>
<!-- 彩色日志 -->
<!-- 彩色日志依赖的渲染类 -->
<conversionRule conversionWord="clr" converterClass="org.springframework.boot.logging.logback.ColorConverter" />
<conversionRule conversionWord="wex" converterClass="org.springframework.boot.logging.logback.WhitespaceThrowableProxyConverter" />
<conversionRule conversionWord="wEx" converterClass="org.springframework.boot.logging.logback.ExtendedWhitespaceThrowableProxyConverter" />
<!-- 彩色日志格式 -->
<property name="CONSOLE_LOG_PATTERN"
value="[${APP_NAME}:${ServerIP}:${ServerPort}] %clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(%level){blue} %clr(${PID}){magenta} %clr([%X{traceId}]){yellow} %clr([%thread]){orange} %clr(%logger){cyan} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}" />
<property name="CONSOLE_LOG_PATTERN_NO_COLOR" value="[${APP_NAME}:${ServerIP}:${ServerPort}] %d{yyyy-MM-dd HH:mm:ss.SSS} %level ${PID} [%X{traceId}] [%thread] %logger %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}" />
<!-- 控制台日志 -->
<appender name="StdoutAppender" class="ch.qos.logback.core.ConsoleAppender">
<withJansi>true</withJansi>
<encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
<layout class="org.apache.skywalking.apm.toolkit.log.logback.v1.x.TraceIdPatternLogbackLayout">
<Pattern>${CONSOLE_LOG_PATTERN}</Pattern>
</layout>
</encoder>
</appender>
<!-- **************************按照每天生成常规日志文件********************** -->
<appender name="FileAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_FILE}/${APP_NAME}.log</file>
<encoder>
<pattern>${CONSOLE_LOG_PATTERN_NO_COLOR}</pattern>
<charset>UTF-8</charset>
</encoder>
<!--基于时间的分包策略 -->
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>${LOG_FILE}/${APP_NAME}.%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<!--保留时间,单位:天-->
<MaxHistory>${LOG_FILEMAXDAY}</MaxHistory>
<MaxFileSize>${LOG_MAXFILESIZE}</MaxFileSize>
</rollingPolicy>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>DEBUG</level>
</filter>
</appender>
<appender name="file_async" class="ch.qos.logback.classic.AsyncAppender">
<discardingThreshold>0</discardingThreshold>
<appender-ref ref="FileAppender"/>
</appender>
<!-- **************************按照每天生成常规日志文件 end********************** -->
<!-- *************************************** 埋点日志 start *************************************** -->
<appender name="point_log" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_POINT_FILE}/point.${APP_NAME}.log</file>
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS}|${APP_NAME}|%msg%n</pattern>
<charset>UTF-8</charset>
</encoder>
<!--基于时间的分包策略 -->
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<FileNamePattern>${LOG_POINT_FILE}/point.${APP_NAME}.%d{yyyy-MM-dd}.%i.log</FileNamePattern>
<!--保留时间,单位:天-->
<MaxHistory>${LOG_FILEMAXDAY}</MaxHistory>
<MaxFileSize>${LOG_MAXFILESIZE}</MaxFileSize>
</rollingPolicy>
</appender>
<appender name="point_log_async" class="ch.qos.logback.classic.AsyncAppender">
<discardingThreshold>0</discardingThreshold>
<appender-ref ref="point_log"/>
</appender>
<!-- *************************************** 埋点日志 end *************************************** -->
<appender name="audit_log" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_AUDIT_FILE}/audit.log</file>
<encoder>
<pattern>%msg%n</pattern>
<charset>UTF-8</charset>
</encoder>
<!-- 基于时间的分包策略 -->
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${LOG_AUDIT_FILE}/audit.%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<!--保留时间,单位:天-->
<maxHistory>${LOG_FILEMAXDAY}</maxHistory>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>${LOG_MAXFILESIZE}</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
</appender>
<appender name="audit_log_async" class="ch.qos.logback.classic.AsyncAppender">
<discardingThreshold>0</discardingThreshold>
<appender-ref ref="audit_log"/>
</appender>
<!-- *************************************** logger配置 start *************************************** -->
<logger name="cn.onehome.dubbocore.log.monitor" level="INFO" addtivity="false">
<appender-ref ref="point_log_async" />
</logger>
<logger name="cn.onehome.dubbocore.log.service.impl.LoggerAuditServiceImpl" level="INFO" addtivity="false">
<appender-ref ref="audit_log_async" />
</logger>
<!-- 多环境定制logger配置(根据spring.profiles.active值对应激活某个配置) -->
<springProfile name="dev">
<root level="INFO">
<appender-ref ref="StdoutAppender"/>
<appender-ref ref="file_async"/>
</root>
</springProfile>
<springProfile name="test">
<root level="INFO">
<appender-ref ref="StdoutAppender"/>
<appender-ref ref="file_async"/>
</root>
</springProfile>
<springProfile name="pre">
<root level="INFO">
<appender-ref ref="StdoutAppender"/>
<appender-ref ref="file_async"/>
</root>
</springProfile>
<springProfile name="prod">
<root level="INFO">
<appender-ref ref="StdoutAppender"/>
<appender-ref ref="file_async"/>
</root>
</springProfile>
<!-- *************************************** logger配置 end *************************************** -->
</configuration>
- 配置拦截器添加追踪日志参数
package cn.wje.common.filter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.MDC;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotNull;
import java.io.IOException;
import java.util.UUID;
/**
* @author luoqifeng
* @date 2023/05/18 10:13
*/
@Slf4j
@Component
@Order(Ordered.HIGHEST_PRECEDENCE)
public class AbstractTraceIdFilter implements Filter {
/**
* 放置traceId
*/
private void putTraceId(String traceId) {
// 放入日志请求域
MDC.put("traceId", traceId);
}
/**
* 清除traceId
*/
private void clearTraceId() {
try {
MDC.remove("traceId");
} catch (Exception e) {
log.warn("clear traceId error. " + e.getMessage());
// ignore
}
}
/**
* 生成traceId
*
* @param request
* 请求
* @return traceId
*/
@NotNull
public static String generateTraceId(HttpServletRequest request) {
String traceId = UUID.randomUUID().toString().replace("-", "");
try {
if (request != null) {
String requestUri = request.getRequestURI();
if (requestUri != null) {
if (requestUri.startsWith("/")) {
requestUri = requestUri.substring(1);
}
if (requestUri.endsWith("/")) {
requestUri = requestUri.substring(0, requestUri.length() - 1);
}
requestUri = requestUri.replace("/", ".");
}
// TODO 如果需要加上用户追踪,这儿可以将用户ID放入 traceId 中
// String accountId = null;
// TODO 获取用户ID逻辑
// traceId = String.join("_", traceId, accountId == null ? "%s" : accountId, requestUri);
traceId = String.join("_", traceId, requestUri);
}
} catch (Exception e) {
// ignore
}
return traceId;
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
try {
HttpServletRequest httpServletRequest = ((HttpServletRequest)request);
String traceId = httpServletRequest.getHeader("traceId");
if (StringUtils.isBlank(traceId)) {
traceId = generateTraceId(httpServletRequest);
log.debug("generate traceId [{}].", traceId);
} else {
log.debug("obtain traceId [{}] from request header. ", traceId);
}
// put
putTraceId(traceId);
chain.doFilter(httpServletRequest, response);
} finally {
// clear
clearTraceId();
}
}
}
- 编写测试接口,运行测试
package cn.wjr.towerJJ.controller;
import cn.wje.common.util.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author luoqifeng
* @date 2023/05/18 10:13
*/
@RestController
@RequestMapping("test")
@Slf4j
public class testController {
@RequestMapping("test001")
public Result<String> test001(){
log.info("test001");
return Result.success("test001");
}
}
-
查看返回与结果:
响应结果
-
贴上我自己封装的返回体,可以根据项目情况自行封装返回体
package cn.wje.common.util;
import cn.wje.common.constants.CommonErrorCode;
import org.slf4j.MDC;
import java.io.Serializable;
/**
* @author luoqifeng
* @date 2023/05/18 10:13
*/
public class Result<T> implements Serializable {
private static final long serialVersionUID = -2234571636295254776L;
/**编码**/
private String code;
/**失败时为失败原因**/
private String msg;
/**成功时的返回数据**/
private T data;
/**
* traceId
* */
private String traceId;
public Result() {
}
/**
* 返回成功
*
* @param data
* @return
*/
public static <T> Result<T> success(T data) {
Result<T> result = new Result<>();
result.setCode(CommonErrorCode.SUCCESS.getCode());
if (data != null) {
result.setData(data);
}
result.setTraceId(MDC.get("traceId"));
return result;
}
/**
* 返回成功
*
* @return
*/
public static <T> Result<T> success() {
return Result.success(null);
}
/**
* 返回失败
*
* @return
*/
public static <T> Result<T> fail(String code,String msg) {
Result<T> result = new Result<>();
result.setCode(code);
result.setMsg(msg);
result.setTraceId(MDC.get("traceId"));
return result;
}
/**
* 返回失败
*
* @return
*/
public static <T> Result<T> fail(CommonErrorCode code) {
return Result.fail(code.getCode(), code.getErrorMsg());
}
/**
* 构建Result
*
* @param code
* @param msg
* @param data
* @return
*/
public static <T> Result<T> build(String code,String msg,T data) {
Result<T> result = new Result<>();
result.setCode(code);
result.setMsg(msg);
result.setData(data);
result.setTraceId("traceId");
return result;
}
/**
* 构建Result
*
* @param code
* @param data
* @return
*/
public static <T> Result<T> build(String code,T data) {
return Result.build(code, null, data);
}
/**
* 构建Result
*
* @param code
* @param msg
* @return
*/
public static <T> Result<T> build(String code,String msg) {
return Result.build(code, msg, null);
}
/**
* 是否成功
*
* @return
*/
public boolean isOk() {
return CommonErrorCode.SUCCESS.getCode().equals(code);
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
public String getTraceId() {
return traceId;
}
public void setTraceId(String traceId) {
this.traceId = traceId;
}
}
- 如果不生效,请断点调试一下拦截器(AbstractTraceIdFilter)是否成功拦截