Skip to content

Commit

Permalink
feat: 系统日志新增 traceId 链路号记录,方便查看完整日志链路
Browse files Browse the repository at this point in the history
  • Loading branch information
Charles7c committed Jan 31, 2024
1 parent b5d668e commit 860ca40
Show file tree
Hide file tree
Showing 11 changed files with 57 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@

package top.charles7c.continew.admin.common.config.tlog;

import com.yomahub.tlog.constant.TLogConstants;
import com.yomahub.tlog.context.TLogContext;
import jakarta.servlet.*;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Component;
import top.charles7c.continew.admin.common.constant.SysConstants;

import java.io.IOException;

Expand All @@ -47,7 +47,7 @@ public void doFilter(ServletRequest request,
try {
TLogWebCommon.loadInstance().preHandle(httpServletRequest);
// 把 traceId 放入 response 的 header,为了方便有些人有这样的需求,从前端拿整条链路的 traceId
httpServletResponse.addHeader(TLogConstants.TLOG_TRACE_KEY, TLogContext.getTraceId());
httpServletResponse.addHeader(SysConstants.TRACE_ID, TLogContext.getTraceId());
chain.doFilter(request, response);
return;
} finally {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package top.charles7c.continew.admin.common.config.tlog;

import com.yomahub.tlog.id.TLogIdGenerator;
import com.yomahub.tlog.id.snowflake.UniqueIdGenerator;

/**
* TLog ID 自定义生成器
Expand All @@ -28,6 +29,6 @@
public class TraceIdGenerator extends TLogIdGenerator {
@Override
public String generateTraceId() {
return String.valueOf(System.nanoTime());
return String.valueOf(UniqueIdGenerator.generateId());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,9 @@ private SysConstants() {
* 描述类字段后缀
*/
public static final String DESCRIPTION_FIELD_SUFFIX = "String";

/**
* Trace ID
*/
public static final String TRACE_ID = "traceId";
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@
import top.charles7c.continew.admin.monitor.model.entity.LogDO;
import top.charles7c.continew.admin.system.service.UserService;
import top.charles7c.continew.starter.core.constant.StringConstants;
import top.charles7c.continew.starter.web.model.R;
import top.charles7c.continew.starter.core.util.ExceptionUtils;
import top.charles7c.continew.starter.log.common.dao.LogDao;
import top.charles7c.continew.starter.log.common.model.LogRecord;
import top.charles7c.continew.starter.log.common.model.LogRequest;
import top.charles7c.continew.starter.log.common.model.LogResponse;
import top.charles7c.continew.starter.web.model.R;

import java.net.URI;
import java.time.LocalDateTime;
Expand Down Expand Up @@ -86,7 +86,9 @@ public void add(LogRecord logRecord) {
LogResponse logResponse = logRecord.getResponse();
Integer statusCode = logResponse.getStatus();
logDO.setStatusCode(statusCode);
logDO.setResponseHeaders(JSONUtil.toJsonStr(logResponse.getHeaders()));
Map<String, String> responseHeaders = logResponse.getHeaders();
logDO.setResponseHeaders(JSONUtil.toJsonStr(responseHeaders));
logDO.setTraceId(responseHeaders.get(SysConstants.TRACE_ID));
String responseBody = logResponse.getBody();
logDO.setResponseBody(responseBody);
// 状态
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,15 @@

package top.charles7c.continew.admin.monitor.model.entity;

import java.io.Serial;
import java.io.Serializable;
import java.time.LocalDateTime;

import lombok.Data;

import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;

import lombok.Data;
import top.charles7c.continew.admin.monitor.enums.LogStatusEnum;

import java.io.Serial;
import java.io.Serializable;
import java.time.LocalDateTime;

/**
* 系统日志实体
*
Expand All @@ -46,6 +44,11 @@ public class LogDO implements Serializable {
@TableId
private Long id;

/**
* 链路 ID
*/
private String traceId;

/**
* 日志描述
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,18 @@

package top.charles7c.continew.admin.monitor.model.query;

import java.io.Serial;
import java.io.Serializable;
import java.util.Date;
import java.util.List;

import lombok.Data;

import cn.hutool.core.date.DatePattern;
import io.swagger.v3.oas.annotations.media.Schema;

import lombok.Data;
import org.springframework.format.annotation.DateTimeFormat;

import cn.hutool.core.date.DatePattern;

import top.charles7c.continew.starter.data.mybatis.plus.query.Query;
import top.charles7c.continew.starter.data.mybatis.plus.query.QueryType;

import java.io.Serial;
import java.io.Serializable;
import java.util.Date;
import java.util.List;

/**
* 系统日志查询条件
*
Expand All @@ -45,6 +41,12 @@ public class SystemLogQuery implements Serializable {
@Serial
private static final long serialVersionUID = 1L;

/**
* 链路 ID
*/
@Schema(description = "链路 ID", example = "904846526308876288")
private String traceId;

/**
* 创建时间
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,10 @@

package top.charles7c.continew.admin.monitor.model.resp;

import java.io.Serial;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

import io.swagger.v3.oas.annotations.media.Schema;
import java.io.Serial;

/**
* 系统日志详情信息
Expand All @@ -35,6 +34,12 @@ public class SystemLogDetailResp extends LogResp {
@Serial
private static final long serialVersionUID = 1L;

/**
* 链路 ID
*/
@Schema(description = "链路 ID", example = "904846526308876288")
private String traceId;

/**
* 状态码
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,10 @@ databaseChangeLog:
- include:
file: db/changelog/v2.2.0/continew-admin_column.sql
- include:
file: db/changelog/v2.2.0/continew-admin_data.sql
file: db/changelog/v2.2.0/continew-admin_data.sql
- include:
file: db/changelog/v2.4.0/continew-admin_table.sql
- include:
file: db/changelog/v2.4.0/continew-admin_column.sql
- include:
file: db/changelog/v2.4.0/continew-admin_data.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
-- liquibase formatted sql

-- changeset Charles7c:1
ALTER TABLE `sys_log` ADD COLUMN `trace_id` varchar(255) NULL COMMENT '链路ID' AFTER `id`;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
-- liquibase formatted sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
-- liquibase formatted sql

0 comments on commit 860ca40

Please sign in to comment.