diff --git a/debug.go b/debug.go index 6911177..b460126 100644 --- a/debug.go +++ b/debug.go @@ -17,7 +17,7 @@ import ( // Author : go_developer@163.com<白茶清欢> // // Date : 1:06 上午 2021/1/3 -func FormatJson(src interface{}) string { +func FormatJson(src any) string { byteData, _ := json.Marshal(src) var str bytes.Buffer diff --git a/go.mod b/go.mod index 423d06d..3ccbfbb 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,8 @@ module git.zhangdeman.cn/zhangdeman/logger go 1.20 require ( - github.com/gin-gonic/gin v1.9.1 + git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240624083652-2598eb986e5b + git.zhangdeman.cn/zhangdeman/gin v0.0.0-20230826052425-e7c13b6089ca github.com/lestrrat-go/file-rotatelogs v2.4.0+incompatible github.com/pkg/errors v0.9.1 go.uber.org/zap v1.27.0 @@ -11,12 +12,12 @@ require ( ) require ( - git.zhangdeman.cn/zhangdeman/gin v0.0.0-20230826052425-e7c13b6089ca // indirect github.com/bytedance/sonic v1.10.0 // indirect github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d // indirect github.com/chenzhuoyu/iasm v0.9.0 // indirect github.com/gabriel-vasile/mimetype v1.4.2 // indirect github.com/gin-contrib/sse v0.1.0 // indirect + github.com/gin-gonic/gin v1.9.1 // indirect github.com/go-playground/locales v0.14.1 // indirect github.com/go-playground/universal-translator v0.18.1 // indirect github.com/go-playground/validator/v10 v10.15.0 // indirect diff --git a/go.sum b/go.sum index 1cdd0c1..9e7e102 100644 --- a/go.sum +++ b/go.sum @@ -1,8 +1,7 @@ -git.zhangdeman.cn/zhangdeman/gin v0.0.0-20230811072113-817a4a831014 h1:Iju6wvcQGxUP9vfOWXmAhnLpqYMJ4YExKvt57alivBM= -git.zhangdeman.cn/zhangdeman/gin v0.0.0-20230811072113-817a4a831014/go.mod h1:IkN3+ZcNpNwyoEeV+dWU8BIu0UNpXcNiqp39yWWqnk0= +git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240624083652-2598eb986e5b h1:vaKXxXJUFvuhgfKXg8BdcsXNg1tDDmyjJX2yQrO/ek8= +git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240624083652-2598eb986e5b/go.mod h1:IXXaZkb7vGzGnGM5RRWrASAuwrVSNxuoe0DmeXx5g6k= git.zhangdeman.cn/zhangdeman/gin v0.0.0-20230826052425-e7c13b6089ca h1:dih0pGEf2snCN7QImFSV62i7jNDKm/kOy/nC4pN9rQI= git.zhangdeman.cn/zhangdeman/gin v0.0.0-20230826052425-e7c13b6089ca/go.mod h1:0qCJAZU+tDC11ZDe2rh8gOfYQhWKlvutNAbs5i4eGfo= -github.com/benbjohnson/clock v1.3.0 h1:ip6w0uFQkncKQ979AypyG0ER7mqUSBdKLOgAle/AT8A= github.com/bytedance/sonic v1.5.0/go.mod h1:ED5hyg4y6t3/9Ku1R6dU/4KyJ48DZ4jPhfY1O2AihPM= github.com/bytedance/sonic v1.10.0-rc/go.mod h1:ElCzW+ufi8qKqNW0FY314xriJhyJhuoJ3gFZdAHF7NM= github.com/bytedance/sonic v1.10.0 h1:qtNZduETEIWJVIyDl01BeNxur2rW9OwTQ/yBqFRkKEk= @@ -83,13 +82,9 @@ github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08= github.com/ugorji/go/codec v1.2.11 h1:BMaWp1Bb6fHwEtbplGBGJ498wD+LKlNSl25MjdZY4dU= github.com/ugorji/go/codec v1.2.11/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg= -go.uber.org/goleak v1.2.0 h1:xqgm/S+aQvhWFTtR0XK3Jvg7z8kGV8P4X14IzwN3Eqk= +go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto= go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0= go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y= -go.uber.org/zap v1.25.0 h1:4Hvk6GtkucQ790dqmj7l1eEnRdKm3k3ZUrUMS2d5+5c= -go.uber.org/zap v1.25.0/go.mod h1:JIAUzQIH94IC4fOJQm7gMmBJP5k7wQfdcnYdPoEXJYk= -go.uber.org/zap v1.26.0 h1:sI7k6L95XOKS281NhVKOFCUNIvv9e0w4BF8N3u+tCRo= -go.uber.org/zap v1.26.0/go.mod h1:dtElttAiwGvoJ/vj4IwHBS/gXsEu/pZ50mUIRWuG0so= go.uber.org/zap v1.27.0 h1:aJMhYGrd5QSmlpLMr2MftRKl7t8J8PTZPA732ud/XR8= go.uber.org/zap v1.27.0/go.mod h1:GB2qFLM7cTU87MWRP2mPIjqfIDnGu+VIO4V/SdhGo2E= golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8= diff --git a/wrapper/gorm_v2.go b/wrapper/gorm_v2.go index 944fc73..581f835 100644 --- a/wrapper/gorm_v2.go +++ b/wrapper/gorm_v2.go @@ -9,12 +9,10 @@ package wrapper import ( "context" - "fmt" + "git.zhangdeman.cn/zhangdeman/consts" "strings" "time" - "github.com/gin-gonic/gin" - "gorm.io/gorm" "go.uber.org/zap/zapcore" @@ -56,7 +54,7 @@ func NewGormV2(loggerLevel zapcore.Level, consoleOutput bool, encoder zapcore.En // Author : go_developer@163.com<白茶清欢> // // Date : 3:36 PM 2021/12/24 -func NewGormLoggerWithInstance(ctx *gin.Context, dbClient *gorm.DB, instance *zap.Logger, node string, extraCtxFieldList []string) logger.Interface { +func NewGormLoggerWithInstance(dbClient *gorm.DB, instance *zap.Logger, node string, extraCtxFieldList []string) logger.Interface { nodeArr := strings.Split(node, "|") i := &Gorm{ dbClient: dbClient, @@ -65,7 +63,6 @@ func NewGormLoggerWithInstance(ctx *gin.Context, dbClient *gorm.DB, instance *za extraCtxFieldList: extraCtxFieldList, flag: "", node: node, - ctx: ctx, } if len(nodeArr) >= 2 { i.node = nodeArr[1] @@ -81,12 +78,11 @@ func NewGormLoggerWithInstance(ctx *gin.Context, dbClient *gorm.DB, instance *za // Date : 9:55 下午 2021/3/1 type Gorm struct { dbClient *gorm.DB - instance *zap.Logger // 日志实例 - traceIDField string // 串联请求上下文的的ID - extraCtxFieldList []string // 从请求上线问提取的字段 - flag string // 数据库标识 - node string // 数据库节点 master / slave - ctx *gin.Context // gin上下文 + instance *zap.Logger // 日志实例 + traceIDField string // 串联请求上下文的的ID + extraCtxFieldList []string // 从请求上线问提取的字段 + flag string // 数据库标识 + node string // 数据库节点 master / slave } // LogMode ... @@ -103,8 +99,8 @@ func (g *Gorm) LogMode(level logger.LogLevel) logger.Interface { // Author : go_developer@163.com<白茶清欢> // // Date : 10:18 下午 2021/3/1 -func (g *Gorm) Info(ctx context.Context, s string, i ...interface{}) { - g.write(nil, "info") +func (g *Gorm) Info(ctx context.Context, s string, i ...any) { + g.write(ctx, s, consts.LogLevelInfo, []zap.Field{zap.Any("log_data", i)}) } // Warn ... @@ -112,8 +108,8 @@ func (g *Gorm) Info(ctx context.Context, s string, i ...interface{}) { // Author : go_developer@163.com<白茶清欢> // // Date : 10:16 下午 2021/3/1 -func (g *Gorm) Warn(ctx context.Context, s string, i ...interface{}) { - g.write(nil, "warn") +func (g *Gorm) Warn(ctx context.Context, s string, i ...any) { + g.write(ctx, s, consts.LogLevelWarn, []zap.Field{zap.Any("log_data", i)}) } // Error 日志 @@ -121,8 +117,8 @@ func (g *Gorm) Warn(ctx context.Context, s string, i ...interface{}) { // Author : go_developer@163.com<白茶清欢> // // Date : 10:18 下午 2021/3/1 -func (g *Gorm) Error(ctx context.Context, s string, i ...interface{}) { - g.write(nil, "error") +func (g *Gorm) Error(ctx context.Context, s string, i ...any) { + g.write(ctx, s, consts.LogLevelError, []zap.Field{zap.Any("log_data", i)}) } // Trace Trace 记录 @@ -144,15 +140,12 @@ func (g *Gorm) Trace(ctx context.Context, begin time.Time, fc func() (string, in zap.String("db_node", g.node), zap.Int64("begin_time", start), zap.Int64("finish_time", end), - zap.String("used_time", fmt.Sprintf("%fms", float64(end-start)/1e6)), + zap.Any("used_time", (end-start)/1e6), zap.String("sql", sql), zap.Int64("affect_rows", affectRows), zap.Error(err), } - if len(g.traceIDField) > 0 { - dataList = append(dataList, zap.String(g.traceIDField, g.getTraceID(ctx))) - } - g.write(dataList, "info") + g.write(ctx, "SQL执行记录", consts.LogLevelInfo, dataList) } // write ... @@ -160,7 +153,10 @@ func (g *Gorm) Trace(ctx context.Context, begin time.Time, fc func() (string, in // Author : go_developer@163.com<白茶清欢> // // Date : 4:11 PM 2021/12/24 -func (g *Gorm) write(dataList []zap.Field, level string) { +func (g *Gorm) write(ctx context.Context, message string, level string, dataList []zap.Field) { + if len(message) == 0 { + message = "SQL执行记录" + } if nil == g.instance { // 未设置日志实例 return @@ -168,40 +164,32 @@ func (g *Gorm) write(dataList []zap.Field, level string) { if nil == dataList { dataList = make([]zap.Field, 0) } - if nil != g.ctx { - for _, extraField := range g.extraCtxFieldList { - if len(extraField) == 0 { - continue - } - dataList = append(dataList, zap.Any(extraField, g.ctx.Value(extraField))) + if nil == ctx { + ctx = context.Background() + } + for _, extraField := range g.extraCtxFieldList { + if len(extraField) == 0 { + continue } + dataList = append(dataList, zap.Any(extraField, ctx.Value(extraField))) } - message := "SQL执行记录" - switch strings.ToLower(level) { - case "info": + switch strings.ToUpper(level) { + case consts.LogLevelDebug: + g.instance.Debug(message, dataList...) + case consts.LogLevelInfo: g.instance.Info(message, dataList...) - case "warn": + case consts.LogLevelWarn: g.instance.Warn(message, dataList...) - case "error": + case consts.LogLevelError: g.instance.Error(message, dataList...) + case consts.LogLevelPanic: + g.instance.Panic(message, dataList...) default: g.instance.Info(message, dataList...) } } -// getTraceID 获取traceID -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 10:11 下午 2021/3/1 -func (g *Gorm) getTraceID(ctx context.Context) string { - if nil == g.ctx { - return "" - } - return g.ctx.GetString(g.traceIDField) -} - // GetGormSQL 获取trace fn // // Author : go_developer@163.com<白茶清欢> diff --git a/wrapper/http_gin.go b/wrapper/http_gin.go index 1530d37..cbcc0fa 100644 --- a/wrapper/http_gin.go +++ b/wrapper/http_gin.go @@ -8,10 +8,8 @@ package wrapper import ( - "encoding/json" - + "context" "git.zhangdeman.cn/zhangdeman/logger" - "github.com/gin-gonic/gin" "go.uber.org/zap" "go.uber.org/zap/zapcore" ) @@ -55,18 +53,16 @@ type Gin struct { // Author : go_developer@163.com<白茶清欢> // // Date : 4:13 下午 2021/1/3 -func (gw *Gin) formatFieldList(ginCtx *gin.Context, inputFieldList []zap.Field) []zap.Field { +func (gw *Gin) formatFieldList(ctx context.Context, inputFieldList []zap.Field) []zap.Field { + if nil == ctx { + ctx = context.Background() + } if nil == inputFieldList { inputFieldList = make([]zap.Field, 0) } - if nil != ginCtx { - // 自动扩充抽取字段,字段不存在的话,忽略掉 - for _, extractField := range gw.extractFieldList { - if v, exist := ginCtx.Get(extractField); exist { - byteData, _ := json.Marshal(v) - inputFieldList = append(inputFieldList, zap.String(extractField, string(byteData))) - } - } + // 自动扩充抽取字段,字段不存在的话,忽略掉 + for _, extractField := range gw.extractFieldList { + inputFieldList = append(inputFieldList, zap.Any(extractField, ctx.Value(extractField))) } return inputFieldList } @@ -76,8 +72,8 @@ func (gw *Gin) formatFieldList(ginCtx *gin.Context, inputFieldList []zap.Field) // Author : go_developer@163.com<白茶清欢> // // Date : 4:14 下午 2021/1/3 -func (gw *Gin) Debug(ginCtx *gin.Context, msg string, field ...zap.Field) { - fieldList := gw.formatFieldList(ginCtx, field) +func (gw *Gin) Debug(ctx context.Context, msg string, field ...zap.Field) { + fieldList := gw.formatFieldList(ctx, field) gw.loggerInstance.Debug(msg, fieldList...) } @@ -86,8 +82,8 @@ func (gw *Gin) Debug(ginCtx *gin.Context, msg string, field ...zap.Field) { // Author : go_developer@163.com<白茶清欢> // // Date : 4:28 下午 2021/1/3 -func (gw *Gin) Info(ginCtx *gin.Context, msg string, field ...zap.Field) { - fieldList := gw.formatFieldList(ginCtx, field) +func (gw *Gin) Info(ctx context.Context, msg string, field ...zap.Field) { + fieldList := gw.formatFieldList(ctx, field) gw.loggerInstance.Info(msg, fieldList...) } @@ -96,8 +92,8 @@ func (gw *Gin) Info(ginCtx *gin.Context, msg string, field ...zap.Field) { // Author : go_developer@163.com<白茶清欢> // // Date : 4:29 下午 2021/1/3 -func (gw *Gin) Warn(ginCtx *gin.Context, msg string, field ...zap.Field) { - fieldList := gw.formatFieldList(ginCtx, field) +func (gw *Gin) Warn(ctx context.Context, msg string, field ...zap.Field) { + fieldList := gw.formatFieldList(ctx, field) gw.loggerInstance.Warn(msg, fieldList...) } @@ -106,8 +102,8 @@ func (gw *Gin) Warn(ginCtx *gin.Context, msg string, field ...zap.Field) { // Author : go_developer@163.com<白茶清欢> // // Date : 4:29 下午 2021/1/3 -func (gw *Gin) Error(ginCtx *gin.Context, msg string, field ...zap.Field) { - fieldList := gw.formatFieldList(ginCtx, field) +func (gw *Gin) Error(ctx context.Context, msg string, field ...zap.Field) { + fieldList := gw.formatFieldList(ctx, field) gw.loggerInstance.Error(msg, fieldList...) } @@ -116,8 +112,8 @@ func (gw *Gin) Error(ginCtx *gin.Context, msg string, field ...zap.Field) { // Author : go_developer@163.com<白茶清欢> // // Date : 4:29 下午 2021/1/3 -func (gw *Gin) Panic(ginCtx *gin.Context, msg string, field ...zap.Field) { - fieldList := gw.formatFieldList(ginCtx, field) +func (gw *Gin) Panic(ctx context.Context, msg string, field ...zap.Field) { + fieldList := gw.formatFieldList(ctx, field) gw.loggerInstance.Panic(msg, fieldList...) } @@ -126,8 +122,8 @@ func (gw *Gin) Panic(ginCtx *gin.Context, msg string, field ...zap.Field) { // Author : go_developer@163.com<白茶清欢> // // Date : 4:30 下午 2021/1/3 -func (gw *Gin) DPanic(ginCtx *gin.Context, msg string, field ...zap.Field) { - fieldList := gw.formatFieldList(ginCtx, field) +func (gw *Gin) DPanic(ctx context.Context, msg string, field ...zap.Field) { + fieldList := gw.formatFieldList(ctx, field) gw.loggerInstance.DPanic(msg, fieldList...) }