日志参数和结果转对象

This commit is contained in:
yuguojian
2025-07-09 18:09:37 +08:00
parent 9b838fd5d9
commit fe311de89f

View File

@@ -6,6 +6,7 @@ import (
"github.com/dromara/carbon/v2" "github.com/dromara/carbon/v2"
"github.com/gogf/gf/v2/frame/g" "github.com/gogf/gf/v2/frame/g"
"go.uber.org/zap" "go.uber.org/zap"
"reflect"
) )
type LogType = string type LogType = string
@@ -28,18 +29,6 @@ const (
LogTypeJJT LogType = "jjt" // 交易所 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 type OperatorType = string
const ( const (
@@ -53,72 +42,104 @@ type Operator struct {
OperatorType OperatorType 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) { func ZapDebug(logger *zap.Logger, log *LogInfo) {
handleLog(log) HandleLog(log)
logger.Debug("", zap.Any("log", log)) logger.Debug("", zap.Any("log", log))
} }
func ZapInfo(logger *zap.Logger, log *LogInfo) { func ZapInfo(logger *zap.Logger, log *LogInfo) {
handleLog(log) HandleLog(log)
logger.Info("", zap.Any("log", log)) logger.Info("", zap.Any("log", log))
} }
func ZapWarn(logger *zap.Logger, log *LogInfo) { func ZapWarn(logger *zap.Logger, log *LogInfo) {
handleLog(log) HandleLog(log)
logger.Warn("", zap.Any("log", log)) logger.Warn("", zap.Any("log", log))
} }
func ZapError(logger *zap.Logger, log *LogInfo) { func ZapError(logger *zap.Logger, log *LogInfo) {
handleLog(log) HandleLog(log)
logger.Error("", zap.Any("log", log)) logger.Error("", zap.Any("log", log))
} }
func ZapPanic(logger *zap.Logger, log *LogInfo) { func ZapPanic(logger *zap.Logger, log *LogInfo) {
handleLog(log) HandleLog(log)
logger.Panic("", zap.Any("log", log)) logger.Panic("", zap.Any("log", log))
} }
func ZapFatal(logger *zap.Logger, log *LogInfo) { func ZapFatal(logger *zap.Logger, log *LogInfo) {
handleLog(log) HandleLog(log)
logger.Fatal("", zap.Any("log", log)) logger.Fatal("", zap.Any("log", log))
} }
func handleLog(log *LogInfo) { func HandleLog(log *LogInfo) {
log.At = carbon.Now().ToDateTimeString() log.At = carbon.Now().ToDateTimeString()
log.Timestamp = carbon.Now().Timestamp() log.Timestamp = carbon.Now().Timestamp()
if _, ok := log.Result.(error); ok { // 处理 Param
log.ErrStr = fmt.Sprintf("%+v", log.Result) // 处理 Param
log.Result = nil 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) { func GFInfo(ctx context.Context, log *LogInfo) {
handleLog(log) HandleLog(log)
g.Log().Info(ctx, log) g.Log().Info(ctx, log)
} }
func GFWarning(ctx context.Context, log *LogInfo) { func GFWarning(ctx context.Context, log *LogInfo) {
handleLog(log) HandleLog(log)
g.Log().Warning(ctx, log) g.Log().Warning(ctx, log)
} }
func GFError(ctx context.Context, log *LogInfo) { func GFError(ctx context.Context, log *LogInfo) {
handleLog(log) HandleLog(log)
g.Log().Error(ctx, log) g.Log().Error(ctx, log)
} }
func GFDebug(ctx context.Context, log *LogInfo) { func GFDebug(ctx context.Context, log *LogInfo) {
handleLog(log) HandleLog(log)
g.Log().Debug(ctx, log) g.Log().Debug(ctx, log)
} }
func GFPanic(ctx context.Context, log *LogInfo) { func GFPanic(ctx context.Context, log *LogInfo) {
handleLog(log) HandleLog(log)
g.Log().Panic(ctx, log) g.Log().Panic(ctx, log)
} }
func GFFatal(ctx context.Context, log *LogInfo) { func GFFatal(ctx context.Context, log *LogInfo) {
handleLog(log) HandleLog(log)
g.Log().Fatal(ctx, log) g.Log().Fatal(ctx, log)
} }
@@ -141,3 +162,15 @@ func (l *LogInfo) ZapCommonHandelResult(logger *zap.Logger, result interface{},
ZapInfo(logger, l) 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)}
}
}