Compare commits
8 Commits
Author | SHA1 | Date | |
---|---|---|---|
![]() |
91f65064ff | ||
![]() |
06d888e8de | ||
![]() |
f6c7d66376 | ||
![]() |
7f0c64fd8a | ||
![]() |
093b34f32f | ||
![]() |
33e119864e | ||
![]() |
f6a41a5e7c | ||
![]() |
994ed9e639 |
77
log/log.go
77
log/log.go
@@ -3,10 +3,8 @@ package log
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/dromara/carbon/v2"
|
"github.com/gogf/gf/v2/os/glog"
|
||||||
"github.com/gogf/gf/v2/frame/g"
|
|
||||||
"go.uber.org/zap"
|
"go.uber.org/zap"
|
||||||
"reflect"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type LogType = string
|
type LogType = string
|
||||||
@@ -38,8 +36,8 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type Operator struct {
|
type Operator struct {
|
||||||
ID int // 系统ID设置0
|
ID int `json:"id"` // 系统ID设置0
|
||||||
OperatorType OperatorType
|
OperatorType OperatorType `json:"operator_type"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type LogInfo struct {
|
type LogInfo struct {
|
||||||
@@ -49,19 +47,20 @@ type LogInfo struct {
|
|||||||
Title string `json:"title"` // 必填,标题,做什么事情
|
Title string `json:"title"` // 必填,标题,做什么事情
|
||||||
Param interface{} `json:"param,omitempty"` // 参数,请求参数,传指针
|
Param interface{} `json:"param,omitempty"` // 参数,请求参数,传指针
|
||||||
Result interface{} `json:"result,omitempty"` // 结果,返回值,传指针,如果是报错,传err
|
Result interface{} `json:"result,omitempty"` // 结果,返回值,传指针,如果是报错,传err
|
||||||
ErrStr string `json:"err_str,omitempty"` // 错误信息,不用传入,自动记录
|
|
||||||
At string `json:"at"` // 记录时间,不用传入,自动记录
|
|
||||||
Timestamp int64 `json:"timestamp"` // 时间戳,不用传入,自动记录
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type param struct {
|
type TempGLog struct {
|
||||||
Type string `json:"type"`
|
Time string `json:"Time"`
|
||||||
Value interface{} `json:"value"`
|
TraceId string `json:"TraceId"`
|
||||||
|
Level string `json:"Level"`
|
||||||
|
Content string `json:"Content"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type result struct {
|
type GLog struct {
|
||||||
Type string `json:"type"`
|
Time string `json:"Time"`
|
||||||
Value interface{} `json:"value"`
|
TraceId string `json:"TraceId"`
|
||||||
|
Level string `json:"Level"`
|
||||||
|
Content *LogInfo `json:"Content"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func ZapDebug(logger *zap.Logger, log *LogInfo) {
|
func ZapDebug(logger *zap.Logger, log *LogInfo) {
|
||||||
@@ -95,57 +94,49 @@ func ZapFatal(logger *zap.Logger, log *LogInfo) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func HandleLog(log *LogInfo) {
|
func HandleLog(log *LogInfo) {
|
||||||
log.At = carbon.Now().ToDateTimeString()
|
|
||||||
log.Timestamp = carbon.Now().Timestamp()
|
|
||||||
// 处理 Param
|
|
||||||
// 处理 Param
|
// 处理 Param
|
||||||
log.Param = processValue(log.Param)
|
log.Param = processValue(log.Param)
|
||||||
// 处理 Result
|
// 处理 Result
|
||||||
if _, ok := log.Result.(error); ok {
|
|
||||||
log.ErrStr = fmt.Sprintf("%+v", log.Result)
|
|
||||||
log.Result = struct{}{}
|
|
||||||
} else {
|
|
||||||
log.Result = processValue(log.Result)
|
log.Result = processValue(log.Result)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
func GFInfo(ctx context.Context, log *LogInfo) {
|
func GFInfo(ctx context.Context, g *glog.Logger, log *LogInfo) {
|
||||||
HandleLog(log)
|
HandleLog(log)
|
||||||
g.Log().Info(ctx, log)
|
g.Info(ctx, log)
|
||||||
}
|
}
|
||||||
|
|
||||||
func GFWarning(ctx context.Context, log *LogInfo) {
|
func GFWarning(ctx context.Context, g *glog.Logger, log *LogInfo) {
|
||||||
HandleLog(log)
|
HandleLog(log)
|
||||||
g.Log().Warning(ctx, log)
|
g.Warning(ctx, log)
|
||||||
}
|
}
|
||||||
|
|
||||||
func GFError(ctx context.Context, log *LogInfo) {
|
func GFError(ctx context.Context, g *glog.Logger, log *LogInfo) {
|
||||||
HandleLog(log)
|
HandleLog(log)
|
||||||
g.Log().Error(ctx, log)
|
g.Error(ctx, log)
|
||||||
}
|
}
|
||||||
|
|
||||||
func GFDebug(ctx context.Context, log *LogInfo) {
|
func GFDebug(ctx context.Context, g *glog.Logger, log *LogInfo) {
|
||||||
HandleLog(log)
|
HandleLog(log)
|
||||||
g.Log().Debug(ctx, log)
|
g.Debug(ctx, log)
|
||||||
}
|
}
|
||||||
|
|
||||||
func GFPanic(ctx context.Context, log *LogInfo) {
|
func GFPanic(ctx context.Context, g *glog.Logger, log *LogInfo) {
|
||||||
HandleLog(log)
|
HandleLog(log)
|
||||||
g.Log().Panic(ctx, log)
|
g.Panic(ctx, log)
|
||||||
}
|
}
|
||||||
|
|
||||||
func GFFatal(ctx context.Context, log *LogInfo) {
|
func GFFatal(ctx context.Context, g *glog.Logger, log *LogInfo) {
|
||||||
HandleLog(log)
|
HandleLog(log)
|
||||||
g.Log().Fatal(ctx, log)
|
g.Fatal(ctx, log)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *LogInfo) GFCommonHandelResult(ctx context.Context, result interface{}, err error) {
|
func (l *LogInfo) GFCommonHandelResult(ctx context.Context, g *glog.Logger, result interface{}, err error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
l.Result = err
|
l.Result = err
|
||||||
GFError(ctx, l)
|
GFError(ctx, g, l)
|
||||||
} else {
|
} else {
|
||||||
l.Result = result
|
l.Result = result
|
||||||
GFInfo(ctx, l)
|
GFInfo(ctx, g, l)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -161,15 +152,7 @@ func (l *LogInfo) ZapCommonHandelResult(logger *zap.Logger, result interface{},
|
|||||||
|
|
||||||
func processValue(value interface{}) interface{} {
|
func processValue(value interface{}) interface{} {
|
||||||
if value == nil {
|
if value == nil {
|
||||||
return struct{}{}
|
return ""
|
||||||
}
|
|
||||||
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)}
|
|
||||||
}
|
}
|
||||||
|
return fmt.Sprintf("%+v", value)
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user