feat(log): 增强日志系统功能与追踪能力

- 新增模块级别日志控制和采样配置
- 为操作日志添加requestId字段支持全链路追踪
- 优化请求日志中间件,增强结构化记录能力
- 实现日志降级机制,数据库不可用时写入文件
- 添加相关数据库迁移脚本和模型索引
This commit is contained in:
zhang1106
2026-05-07 17:14:54 +08:00
parent 54b0a8e7bb
commit 03e320af9e
8 changed files with 2809 additions and 58 deletions
+12
View File
@@ -1,3 +1,9 @@
/**
* 操作日志模型
* 支持请求追踪IDrequestId)关联
* 支持复合索引优化查询性能
*/
const { DataTypes } = require('sequelize');
const { sequelize } = require('../db');
@@ -72,6 +78,10 @@ const OperationLog = sequelize.define(
type: DataTypes.STRING,
comment: '用户代理',
},
requestId: {
type: DataTypes.STRING,
comment: '请求追踪ID,关联HTTP请求日志',
},
metadata: {
type: DataTypes.JSON,
defaultValue: {},
@@ -87,6 +97,8 @@ const OperationLog = sequelize.define(
{ fields: ['targetId'] },
{ fields: ['operatorId'] },
{ fields: ['createdAt'] },
{ fields: ['requestId'] },
{ fields: ['module', 'createdAt'] },
],
}
);