From fe311de89fcc352eb3d02263277ff4eb8a8ba336 Mon Sep 17 00:00:00 2001 From: yuguojian <18126816215> Date: Wed, 9 Jul 2025 18:09:37 +0800 Subject: [PATCH] =?UTF-8?q?=E6=97=A5=E5=BF=97=E5=8F=82=E6=95=B0=E5=92=8C?= =?UTF-8?q?=E7=BB=93=E6=9E=9C=E8=BD=AC=E5=AF=B9=E8=B1=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- log/log.go | 89 +++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 61 insertions(+), 28 deletions(-) diff --git a/log/log.go b/log/log.go index ac57e4d..b587d2f 100644 --- a/log/log.go +++ b/log/log.go @@ -6,6 +6,7 @@ import ( "github.com/dromara/carbon/v2" "github.com/gogf/gf/v2/frame/g" "go.uber.org/zap" + "reflect" ) type LogType = string @@ -28,18 +29,6 @@ const ( LogTypeJJT LogType = "jjt" // 交易所 ) -type LogInfo struct { - LogType LogType `json:"log_type"` // 日志类型,按被操作对象划分 - Operator *Operator `json:"operator,omitempty"` // 操作人,系统操作不用填写操作人,写日志类型就可以 - ObjectorID int `json:"objector_id,omitempty"` // 被操作对象ID,如果是gps,短信等服务则为0 - Title string `json:"title"` // 必填,标题,做什么事情 - Param interface{} `json:"param,omitempty"` // 参数,请求参数,传指针 - Result interface{} `json:"result,omitempty"` // 结果,返回值,传指针,如果是报错,传err - ErrStr string `json:"err_str,omitempty"` // 错误信息,不用传入,自动记录 - At string `json:"at"` // 记录时间,不用传入,自动记录 - Timestamp int64 `json:"timestamp"` // 时间戳,不用传入,自动记录 -} - type OperatorType = string const ( @@ -53,72 +42,104 @@ type Operator struct { OperatorType OperatorType } +type LogInfo struct { + LogType LogType `json:"log_type"` // 日志类型,按被操作对象划分 + Operator *Operator `json:"operator,omitempty"` // 操作人,系统操作不用填写操作人,写日志类型就可以 + ObjectorID int `json:"objector_id,omitempty"` // 被操作对象ID,如果是gps,短信等服务则为0 + Title string `json:"title"` // 必填,标题,做什么事情 + Param interface{} `json:"param,omitempty"` // 参数,请求参数,传指针 + Result interface{} `json:"result,omitempty"` // 结果,返回值,传指针,如果是报错,传err + ErrStr string `json:"err_str,omitempty"` // 错误信息,不用传入,自动记录 + At string `json:"at"` // 记录时间,不用传入,自动记录 + Timestamp int64 `json:"timestamp"` // 时间戳,不用传入,自动记录 +} + +type param struct { + Type string `json:"type"` + Value interface{} `json:"value"` +} + +type result struct { + Type string `json:"type"` + Value interface{} `json:"value"` +} + func ZapDebug(logger *zap.Logger, log *LogInfo) { - handleLog(log) + HandleLog(log) logger.Debug("", zap.Any("log", log)) } func ZapInfo(logger *zap.Logger, log *LogInfo) { - handleLog(log) + HandleLog(log) logger.Info("", zap.Any("log", log)) } func ZapWarn(logger *zap.Logger, log *LogInfo) { - handleLog(log) + HandleLog(log) logger.Warn("", zap.Any("log", log)) } func ZapError(logger *zap.Logger, log *LogInfo) { - handleLog(log) + HandleLog(log) logger.Error("", zap.Any("log", log)) } func ZapPanic(logger *zap.Logger, log *LogInfo) { - handleLog(log) + HandleLog(log) logger.Panic("", zap.Any("log", log)) } func ZapFatal(logger *zap.Logger, log *LogInfo) { - handleLog(log) + HandleLog(log) logger.Fatal("", zap.Any("log", log)) } -func handleLog(log *LogInfo) { +func HandleLog(log *LogInfo) { log.At = carbon.Now().ToDateTimeString() log.Timestamp = carbon.Now().Timestamp() - if _, ok := log.Result.(error); ok { - log.ErrStr = fmt.Sprintf("%+v", log.Result) - log.Result = nil + // 处理 Param + // 处理 Param + if log.Param != nil { + log.Param = processValue(log.Param) + } + // 处理 Result + if log.Result != nil { + if _, ok := log.Result.(error); ok { + log.ErrStr = fmt.Sprintf("%+v", log.Result) + log.Result = nil + } else { + log.Result = processValue(log.Result) + } } } func GFInfo(ctx context.Context, log *LogInfo) { - handleLog(log) + HandleLog(log) g.Log().Info(ctx, log) } func GFWarning(ctx context.Context, log *LogInfo) { - handleLog(log) + HandleLog(log) g.Log().Warning(ctx, log) } func GFError(ctx context.Context, log *LogInfo) { - handleLog(log) + HandleLog(log) g.Log().Error(ctx, log) } func GFDebug(ctx context.Context, log *LogInfo) { - handleLog(log) + HandleLog(log) g.Log().Debug(ctx, log) } func GFPanic(ctx context.Context, log *LogInfo) { - handleLog(log) + HandleLog(log) g.Log().Panic(ctx, log) } func GFFatal(ctx context.Context, log *LogInfo) { - handleLog(log) + HandleLog(log) g.Log().Fatal(ctx, log) } @@ -141,3 +162,15 @@ func (l *LogInfo) ZapCommonHandelResult(logger *zap.Logger, result interface{}, ZapInfo(logger, l) } } + +func processValue(value interface{}) interface{} { + kind := reflect.TypeOf(value).Kind() + switch kind { + case reflect.Map, reflect.Slice, reflect.Array, reflect.Struct: + // 如果是复杂类型,保持原样 + return value + default: + // 对于其他类型,转换为字符串 + return param{Type: kind.String(), Value: fmt.Sprintf("%v", value)} + } +}