日志参数和结果转对象

This commit is contained in:
yuguojian
2025-07-09 18:05:14 +08:00
parent 9b838fd5d9
commit a5cb0496c0
2 changed files with 61 additions and 40 deletions

12
go.sum
View File

@@ -1,12 +0,0 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/dromara/carbon/v2 v2.6.4 h1:cpIansyiEIEed3OlEIqo1IXj86qu0x6pf/E2keL2wYo=
github.com/dromara/carbon/v2 v2.6.4/go.mod h1:Baj3A1uBBctJmpZWJd6/+WWnmIuY2pobR6IOpB6xigc=
github.com/gogf/gf/v2 v2.7.2/go.mod h1:EBXneAg/wes86rfeh68XC0a2JBNQylmT7Sp6/8Axk88=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
go.uber.org/multierr v1.10.0 h1:S0h4aNzvfcFsC3dRF1jLoaov7oRaKqRGC/pUEJ2yvPQ=
go.uber.org/multierr v1.10.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
go.uber.org/zap v1.27.0 h1:aJMhYGrd5QSmlpLMr2MftRKl7t8J8PTZPA732ud/XR8=
go.uber.org/zap v1.27.0/go.mod h1:GB2qFLM7cTU87MWRP2mPIjqfIDnGu+VIO4V/SdhGo2E=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=

View File

@@ -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()
// 处理 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)}
}
}