diff --git a/log/model/dto/log_dto.go b/log/model/dto/log_dto.go index 862e91c..fab47af 100644 --- a/log/model/dto/log_dto.go +++ b/log/model/dto/log_dto.go @@ -48,12 +48,3 @@ type ListLogsResp struct { Logs []OperationLogInfo `json:"logs" dc:"日志列表"` Total int64 `json:"total" dc:"总数"` } - -// ========== 记录操作日志DTO ========== - -// RecordCreateLogReq 记录创建操作日志请求 -type RecordCreateLogReq struct { - ServiceName string `json:"service_name" v:"required" dc:"服务名"` - Collection string `json:"collection" v:"required" dc:"数据所在集合名称"` - Data []interface{} `json:"data" dc:"当前数据"` -} diff --git a/log/model/entity/log.go b/log/model/entity/log.go index ca59c9b..ac566a4 100644 --- a/log/model/entity/log.go +++ b/log/model/entity/log.go @@ -8,11 +8,10 @@ import ( type OperationLog struct { beans.MongoBaseDO `bson:",inline"` - 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等 - Operation string `bson:"operation" json:"operation"` // 操作类型:create, update, delete - UserName string `bson:"user_name" json:"user_name"` // 操作人名称 - IPAddress string `bson:"ip_address" json:"ip_address"` // 操作IP地址 - Data map[string]interface{} `bson:"data,omitempty" json:"data"` // 当前数据:操作时的数据状态 + 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等 + 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"` // 当前数据:操作时的数据状态 } diff --git a/log/service/log_service.go b/log/service/log_service.go index 70fcac4..73a92d6 100644 --- a/log/service/log_service.go +++ b/log/service/log_service.go @@ -7,7 +7,6 @@ import ( "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" - "gitee.com/red-future---jilin-g/common/utils" "github.com/gogf/gf/v2/frame/g" "github.com/gogf/gf/v2/util/gconv" ) @@ -75,26 +74,14 @@ func (s *operationLog) List(ctx context.Context, req *dto.ListLogsReq) ([]dto.Op // record 记录操作日志的通用方法 func (s *operationLog) record(ctx context.Context, serviceName, collection, collectionID, operation string, data map[string]interface{}) error { - // 获取用户信息 - user, err := utils.GetUserInfo(ctx) - if err != nil { - return err - } - // 获取请求信息 ipAddress := getHTTPRequestInfo(ctx) - var userName string - if user.UserName != nil { - userName = gconv.String(user.UserName) - } - log := &logEntity.OperationLog{ ServiceName: serviceName, Collection: collection, CollectionID: collectionID, Operation: operation, - UserName: userName, IPAddress: ipAddress, Data: data, } diff --git a/mongo/mongo.go b/mongo/mongo.go index 3f2141a..773bd10 100644 --- a/mongo/mongo.go +++ b/mongo/mongo.go @@ -9,7 +9,7 @@ import ( "time" "gitee.com/red-future---jilin-g/common/beans" - "gitee.com/red-future---jilin-g/common/log/model/dto" + "gitee.com/red-future---jilin-g/common/log/model/entity" "github.com/gogf/gf/v2/container/gvar" "github.com/gogf/gf/v2/os/grpool" @@ -178,7 +178,7 @@ var logPool *grpool.Pool // init 初始化MongoDB连接 func init() { - logPool = grpool.New(10) + logPool = grpool.New(1) // 按需初始化:没有配置 mongo.address 则跳过 mongoAddr = g.Cfg().MustGet(context.Background(), "mongo.address").String() if mongoAddr == "" { @@ -390,6 +390,33 @@ func (m *MongoDB) CleanRedis(ctx context.Context, filter bson.M, tenantId interf return } +var serverName = g.Cfg().MustGet(context.TODO(), "server.name").String() +var logRedisKey = fmt.Sprintf("log:%s", serverName) + +func (m *MongoDB) log(ctx context.Context, filter bson.M, collection string, data interface{}, userName, tenantId interface{}, operationType string) { + _ = logPool.AddWithRecover(ctx, func(ctx context.Context) { + log := &entity.OperationLog{ + ServiceName: serverName, + Collection: collection, + CollectionID: filter["_id"].(string), + Operation: operationType, + IPAddress: g.RequestFromCtx(ctx).GetClientIp(), + Data: data, + } + log.Creator = userName + now := >ime.Now().Time + log.CreatedAt = now + log.UpdatedAt = now + log.TenantId = tenantId + if _, err := redis.AddToStream(ctx, logRedisKey, log); err != nil { + glog.Error(ctx, "mongoLog-AddToStream err: %v", err) + } + }, func(ctx context.Context, exception error) { + glog.Error(ctx, "mongoLog-AddWithRecover err: %v", exception) + }) + return +} + // Delete 删除记录 func (m *MongoDB) Delete(ctx context.Context, filter bson.M, collection string, opts ...options.Lister[options.DeleteManyOptions]) (count int64, err error) { if len(filter) == 0 { @@ -408,19 +435,7 @@ func (m *MongoDB) Delete(ctx context.Context, filter bson.M, collection string, count = r.DeletedCount err = m.CleanRedis(ctx, filter, user.TenantId, collection) //写日志 - var rows []interface{} - if _, err = m.Find(ctx, filter, &rows, collection, nil, nil); err != nil { - return - } - serverName := g.Cfg().MustGet(ctx, "server.name").String() - logRedisKey := fmt.Sprintf("log:%s", serverName) - if _, err = redis.AddToStream(ctx, logRedisKey, &dto.RecordCreateLogReq{ - ServiceName: serverName, - Collection: collection, - Data: rows, - }); err != nil { - glog.Error(ctx, "mongoLog-AddToStream err: %v", err) - } + m.log(ctx, filter, collection, nil, user.UserName, user.TenantId, "delete") return } @@ -450,19 +465,7 @@ func (m *MongoDB) Update(ctx context.Context, filter bson.M, update bson.M, coll } err = m.CleanRedis(ctx, filter, user.TenantId, collection) //写日志 - serverName := g.Cfg().MustGet(ctx, "server.name").String() - logRedisKey := fmt.Sprintf("log:%s", serverName) - var rows []interface{} - if _, err = m.Find(ctx, filter, &rows, collection, nil, nil); err != nil { - return - } - if _, err = redis.AddToStream(ctx, logRedisKey, &dto.RecordCreateLogReq{ - ServiceName: serverName, - Collection: collection, - Data: rows, - }); err != nil { - glog.Error(ctx, "mongoLog-AddToStream err: %v", err) - } + m.log(ctx, filter, collection, update, user.UserName, user.TenantId, "update") return } @@ -619,29 +622,7 @@ func (m *MongoDB) Insert(ctx context.Context, documents []interface{}, collectio ids = r.InsertedIDs err = m.CleanRedis(ctx, bson.M{}, user.TenantId, collection) //写日志 - serverName := g.Cfg().MustGet(ctx, "server.name").String() - logRedisKey := fmt.Sprintf("log:%s", serverName) - if len(ids) == 0 { - return - } - rows := make([]interface{}, 0, len(ids)) - if len(ids) == 1 { - doc := gconv.Map(documents[0]) - doc["id"] = ids[0] - rows = append(rows, doc) - } else { - filter := bson.M{"_id": bson.M{"$in": ids}} - if _, err = m.Find(ctx, filter, &rows, collection, nil, nil); err != nil { - return - } - } - if _, err = redis.AddToStream(ctx, logRedisKey, &dto.RecordCreateLogReq{ - ServiceName: serverName, - Collection: collection, - Data: rows, - }); err != nil { - glog.Error(ctx, "mongoLog-AddToStream err: %v", err) - } + m.log(ctx, nil, collection, ids, user.UserName, user.TenantId, "insert") return }