优化日志模块:重构操作日志记录与查询功能,支持批量操作日志记录,完善日志查询接口,增加软删除操作类型

This commit is contained in:
2026-01-16 16:55:32 +08:00
committed by 张斌
parent cc940c27b7
commit f0e6bdd37c
8 changed files with 260 additions and 300 deletions

View File

@@ -4,12 +4,20 @@ package consts
type OperationType string
const (
OperationCreate OperationType = "create" // 创建
OperationUpdate OperationType = "update" // 更新
OperationDelete OperationType = "delete" // 删除
OperationInsert OperationType = "insert" // 创建
OperationUpdate OperationType = "update" // 更新
OperationDelete OperationType = "delete" // 删除
OperationDeleteSoft OperationType = "delete_soft" // 软删除
)
// OperationLogCollection 操作日志集合名称常量
const (
OperationLogCollection = "operation_logs" // 操作日志集合名称
)
// 消费者配置(从 Redis Stream 消费请求)
const StreamKey = "log:%s" // 请求 Stream 键名与发消息的key一致
const GroupName = "log:consumer:group" // 消费者组名
const ConsumerName = "message-consumer-1" // 消费者名称(唯一标识)
const BatchSize = 1 // 批处理大小每次读取1条
const AutoAck = true // ACK是否自动确认true自动确认false不确认

View File

@@ -12,33 +12,7 @@ type operationLog struct{}
// OperationLog 操作日志控制器
var OperationLog = new(operationLog)
// GetByID 根据ID获取操作日志
// @Summary 获取操作日志详情
// @Description 根据日志ID获取操作日志的详细信息
func (c *operationLog) GetByID(ctx context.Context, req *dto.GetLogReq) (res *dto.GetLogResp, err error) {
logInfo, err := service.OperationLog.GetByID(ctx, req.ID)
if err != nil {
return
}
res = &dto.GetLogResp{
OperationLogInfo: *logInfo,
}
return
}
// List 查询操作日志列表(通用方法,支持根据不同条件动态查询)
// @Summary 查询操作日志列表
// @Description 根据多个条件查询操作日志列表
func (c *operationLog) List(ctx context.Context, req *dto.ListLogsReq) (res *dto.ListLogsResp, err error) {
logs, total, err := service.OperationLog.List(ctx, req)
if err != nil {
return
}
res = &dto.ListLogsResp{
Logs: logs,
Total: total,
}
return
// GetByCollectionId 根据collectionId获取操作日志列表
func (c *operationLog) GetByCollectionId(ctx context.Context, req *dto.ListLogsReq) (res *dto.ListLogsResp, err error) {
return service.OperationLog.GetByCollectionId(ctx, req)
}

View File

@@ -2,6 +2,8 @@ package dao
import (
"context"
"gitee.com/red-future---jilin-g/common/beans"
"strings"
"time"
"gitee.com/red-future---jilin-g/common/log/consts"
@@ -9,7 +11,6 @@ import (
"gitee.com/red-future---jilin-g/common/log/model/entity"
"gitee.com/red-future---jilin-g/common/mongo"
"go.mongodb.org/mongo-driver/v2/bson"
"go.mongodb.org/mongo-driver/v2/mongo/options"
)
type log struct{}
@@ -23,75 +24,14 @@ func (d *log) Create(ctx context.Context, log *entity.OperationLog) error {
return err
}
// CreateBatch 批量创建日志记录
func (d *log) CreateBatch(ctx context.Context, logs []*entity.OperationLog) error {
if len(logs) == 0 {
return nil
}
documents := make([]interface{}, len(logs))
for i, log := range logs {
documents[i] = log
}
_, err := mongo.DB().Insert(ctx, documents, consts.OperationLogCollection)
return err
}
// GetByID 根据ID获取日志
func (d *log) GetByID(ctx context.Context, id string) (*entity.OperationLog, error) {
objectID, err := bson.ObjectIDFromHex(id)
if err != nil {
return nil, err
}
filter := bson.M{"_id": objectID}
var log entity.OperationLog
err = mongo.DB().FindOne(ctx, filter, &log, consts.OperationLogCollection)
if err != nil {
return nil, err
}
return &log, nil
}
// List 查询日志列表通用方法通过filter动态拼接查询条件
func (d *log) List(ctx context.Context, filter *dto.ListLogsReq, sortFields ...string) ([]*entity.OperationLog, int64, error) {
bsonFilter := buildFilter(filter)
total, err := mongo.DB().Count(ctx, bsonFilter, consts.OperationLogCollection)
if err != nil {
return nil, 0, err
func (d *log) List(ctx context.Context, req *dto.ListLogsReq) (res []*entity.OperationLog, total int64, err error) {
filter := buildFilter(req)
req.OrderBy = []beans.OrderBy{
{Field: "createdAt", Order: beans.Desc},
}
var findOptions []options.Lister[options.FindOptions]
if filter.PageNum > 0 && filter.PageSize > 0 {
findOptions = append(findOptions, options.Find().SetSkip(int64((filter.PageNum-1)*filter.PageSize)).SetLimit(int64(filter.PageSize)))
}
if len(sortFields) > 0 {
sort := bson.D{}
for _, field := range sortFields {
var order int
if len(field) > 0 && field[0] == '-' {
order = -1
field = field[1:]
} else {
order = 1
}
sort = append(sort, bson.E{Key: field, Value: order})
}
findOptions = append(findOptions, options.Find().SetSort(sort))
} else {
findOptions = append(findOptions, options.Find().SetSort(bson.D{{Key: "createdAt", Value: -1}}))
}
var logs []*entity.OperationLog
_, err = mongo.DB().Find(ctx, bsonFilter, &logs, consts.OperationLogCollection, nil, nil)
if err != nil {
return nil, 0, err
}
return logs, total, nil
total, err = mongo.DB().Find(ctx, filter, &res, consts.OperationLogCollection, req.Page, req.OrderBy)
return
}
// buildFilter 构建MongoDB查询过滤器
@@ -107,12 +47,11 @@ func buildFilter(filter interface{}) bson.M {
bsonFilter["collection"] = req.Collection
}
if req.CollectionID != "" {
bsonFilter["collection_id"] = req.CollectionID
bsonFilter["collection_id"] = bson.M{"$in": strings.Split(req.CollectionID, ",")}
}
if req.Operation != "" {
bsonFilter["operation"] = req.Operation
}
// 处理时间范围字段
if req.StartTime != "" || req.EndTime != "" {
timeFilter := bson.M{}

View File

@@ -3,37 +3,13 @@ package dto
import (
"gitee.com/red-future---jilin-g/common/beans"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/os/gtime"
)
// ========== 操作日志查询相关DTO ==========
// GetLogReq 获取操作日志请求
type GetLogReq struct {
g.Meta `path:"/getLog" method:"get" tags:"操作日志" summary:"获取操作日志详情" dc:"根据日志ID获取操作日志的详细信息"`
ID string `json:"id" v:"required" dc:"日志ID"`
}
// GetLogResp 获取操作日志响应
type GetLogResp struct {
OperationLogInfo
}
// OperationLogInfo 操作日志信息
type OperationLogInfo struct {
ID string `json:"id" dc:"日志ID"`
ServiceName string `json:"service_name" dc:"服务名"`
Collection string `json:"collection" dc:"数据所在集合名称"`
CollectionID string `json:"collection_id" dc:"数据ID"`
Operation string `json:"operation" dc:"操作类型"`
UserName string `json:"user_name" dc:"操作人名称"`
IPAddress string `json:"ip_address" dc:"操作IP地址"`
Data map[string]interface{} `json:"data" dc:"当前数据"`
}
// ListLogsReq 查询操作日志列表请求(通用方法,支持根据不同条件动态查询)
type ListLogsReq struct {
g.Meta `path:"/listLogs" method:"get" tags:"操作日志" summary:"查询操作日志列表" dc:"根据多个条件查询操作日志列表"`
beans.Page
*beans.Page
ServiceName string `json:"service_name" dc:"服务名(可选)"`
Collection string `json:"collection" dc:"数据所在集合名称(可选)"`
CollectionID string `json:"collection_id" dc:"数据ID可选"`
@@ -48,3 +24,16 @@ type ListLogsResp struct {
Logs []OperationLogInfo `json:"logs" dc:"日志列表"`
Total int64 `json:"total" dc:"总数"`
}
// OperationLogInfo 操作日志信息
type OperationLogInfo struct {
ID string `json:"id" dc:"日志ID"`
ServiceName string `json:"service_name" dc:"服务名"`
Collection string `json:"collection" dc:"数据所在集合名称"`
CollectionID interface{} `json:"collection_id" dc:"数据ID"`
Operation string `json:"operation" dc:"操作类型"`
Creator string `json:"creator" dc:"操作人名称"`
CreatedAt *gtime.Time `json:"createdAt" dc:"创建时间"`
Data interface{} `json:"data" dc:"当前数据"`
IPAddress string `json:"ip_address" dc:"操作IP地址"`
}

View File

@@ -10,7 +10,7 @@ type OperationLog struct {
ServiceName string `bson:"service_name" json:"service_name"` // 服务名:具体的微服务名称
Collection string `bson:"collection" json:"collection"` // 集合名:数据所在的集合名称
CollectionID string `bson:"collection_id" json:"collection_id"` // 数据ID具体操作的数据ID如订单号、钱包ID等
CollectionID interface{} `bson:"collection_id" json:"collection_id"` // 数据ID具体操作的数据ID如订单号、钱包ID等
Operation string `bson:"operation" json:"operation"` // 操作类型create, update, delete
IPAddress string `bson:"ip_address" json:"ip_address"` // 操作IP地址
Data interface{} `bson:"data,omitempty" json:"data"` // 当前数据:操作时的数据状态

View File

@@ -2,12 +2,11 @@ package service
import (
"context"
"gitee.com/red-future---jilin-g/common/log/consts"
"gitee.com/red-future---jilin-g/common/beans"
"gitee.com/red-future---jilin-g/common/log/dao"
"gitee.com/red-future---jilin-g/common/log/model/dto"
logEntity "gitee.com/red-future---jilin-g/common/log/model/entity"
"github.com/gogf/gf/v2/frame/g"
"gitee.com/red-future---jilin-g/common/utils"
"github.com/gogf/gf/v2/util/gconv"
)
@@ -16,84 +15,52 @@ type operationLog struct{}
// OperationLog 操作日志服务
var OperationLog = &operationLog{}
// RecordCreate 记录创建操作
func (s *operationLog) RecordCreate(ctx context.Context, serviceName, collection, collectionID string, data map[string]interface{}) error {
return s.record(ctx, serviceName, collection, collectionID, string(consts.OperationCreate), data)
}
// RecordUpdate 记录更新操作
func (s *operationLog) RecordUpdate(ctx context.Context, serviceName, collection, collectionID string, data map[string]interface{}) error {
return s.record(ctx, serviceName, collection, collectionID, string(consts.OperationUpdate), data)
}
// RecordDelete 记录删除操作
func (s *operationLog) RecordDelete(ctx context.Context, serviceName, collection, collectionID string, data map[string]interface{}) error {
return s.record(ctx, serviceName, collection, collectionID, string(consts.OperationDelete), data)
}
// BatchRecordCreate 批量记录创建操作
func (s *operationLog) BatchRecordCreate(ctx context.Context, logs []*logEntity.OperationLog) error {
return dao.Log.CreateBatch(ctx, logs)
}
// GetByID 根据ID获取操作日志
func (s *operationLog) GetByID(ctx context.Context, id string) (*dto.OperationLogInfo, error) {
log, err := dao.Log.GetByID(ctx, id)
if err != nil {
return nil, err
}
var logInfo dto.OperationLogInfo
if err := gconv.Struct(log, &logInfo); err != nil {
return nil, err
}
logInfo.ID = log.Id.Hex()
return &logInfo, nil
}
// List 查询操作日志列表
func (s *operationLog) List(ctx context.Context, req *dto.ListLogsReq) ([]dto.OperationLogInfo, int64, error) {
logs, total, err := dao.Log.List(ctx, req)
if err != nil {
return nil, 0, err
}
var logInfos []dto.OperationLogInfo
err = gconv.Structs(logs, &logInfos)
if err != nil {
return nil, 0, err
}
// 处理特殊字段
for i, log := range logs {
logInfos[i].ID = log.Id.Hex()
}
return logInfos, total, nil
}
// record 记录操作日志的通用方法
func (s *operationLog) record(ctx context.Context, serviceName, collection, collectionID, operation string, data map[string]interface{}) error {
// 获取请求信息
ipAddress := getHTTPRequestInfo(ctx)
func (s *operationLog) AddOperationLog(ctx context.Context, msg map[string]interface{}) error {
serviceName := gconv.String(msg["service_name"])
collection := gconv.String(msg["collection"])
collectionId := gconv.Strings(msg["collection_id"])
operation := gconv.String(msg["operation"])
ipAddress := gconv.String(msg["ip_address"])
data := gconv.Maps(msg["data"])
creator := gconv.String(msg["creator"])
createdAt := gconv.Time(msg["createdAt"])
updater := gconv.String(msg["updater"])
updatedAt := gconv.Time(msg["updatedAt"])
tenantId := gconv.Float64(msg["tenantId"])
// 设置 userId 和 tenantId 到 ctx
ctx = context.WithValue(ctx, "userName", creator)
ctx = context.WithValue(ctx, "tenantId", tenantId)
log := &logEntity.OperationLog{
MongoBaseDO: beans.MongoBaseDO{
Creator: creator,
CreatedAt: &createdAt,
Updater: updater,
UpdatedAt: &updatedAt,
TenantId: tenantId,
},
ServiceName: serviceName,
Collection: collection,
CollectionID: collectionID,
CollectionID: collectionId,
Operation: operation,
IPAddress: ipAddress,
Data: data,
}
return dao.Log.Create(ctx, log)
}
// getHTTPRequestInfo 从上下文中获取HTTP请求信息
func getHTTPRequestInfo(ctx context.Context) string {
request := g.RequestFromCtx(ctx)
if request != nil {
return request.GetClientIp()
// GetByCollectionId 根据集合ID获取操作日志
func (s *operationLog) GetByCollectionId(ctx context.Context, req *dto.ListLogsReq) (res *dto.ListLogsResp, err error) {
logs, total, err := dao.Log.List(ctx, req)
if err != nil {
return
}
return ""
res = &dto.ListLogsResp{
Total: total,
}
err = utils.Struct(logs, &res.Logs)
if err != nil {
return
}
return
}