日志参数和结果转对象
This commit is contained in:
83
log/log.go
83
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()
|
||||
// 处理 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)}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user