From 4566a456d9e77445139f6342984770fdbe31bedf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Sat, 26 Aug 2023 13:07:08 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=97=A5=E5=BF=97BUG?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- go.mod | 1 + go.sum | 2 ++ wrapper/gorm_v2.go | 10 ++++++---- 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/go.mod b/go.mod index ee0855a..792279e 100644 --- a/go.mod +++ b/go.mod @@ -11,6 +11,7 @@ require ( ) require ( + git.zhangdeman.cn/zhangdeman/gin v0.0.0-20230811072113-817a4a831014 // 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 diff --git a/go.sum b/go.sum index fa7129d..f14da04 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,5 @@ +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= 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= diff --git a/wrapper/gorm_v2.go b/wrapper/gorm_v2.go index a52ad90..34e8951 100644 --- a/wrapper/gorm_v2.go +++ b/wrapper/gorm_v2.go @@ -21,6 +21,7 @@ import ( "go.uber.org/zap" + pkgGinDefine "git.zhangdeman.cn/zhangdeman/gin/define" logger2 "git.zhangdeman.cn/zhangdeman/logger" "gorm.io/gorm/logger" ) @@ -60,7 +61,7 @@ func NewGormLoggerWithInstance(ctx *gin.Context, dbClient *gorm.DB, instance *za i := &Gorm{ dbClient: dbClient, instance: instance, - traceIDField: "", + traceIDField: pkgGinDefine.TraceIDField, extraCtxFieldList: extraCtxFieldList, flag: "", node: node, @@ -139,7 +140,6 @@ func (g *Gorm) Trace(ctx context.Context, begin time.Time, fc func() (string, in } dataList := []zap.Field{ - zap.String(g.traceIDField, g.getTraceID(ctx)), zap.String("db_flag", g.flag), zap.Int64("begin_time", start), zap.Int64("finish_time", end), @@ -148,8 +148,10 @@ func (g *Gorm) Trace(ctx context.Context, begin time.Time, fc func() (string, in 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") - } // write ... @@ -177,7 +179,7 @@ func (g *Gorm) write(dataList []zap.Field, level string) { // 补齐 flag、node sql := g.dbClient.Dialector.Explain(g.dbClient.Statement.SQL.String(), g.dbClient.Statement.Vars...) affectRows := g.dbClient.RowsAffected - dataList = append(dataList, zap.String("db_node", g.node), zap.String("db_flag", g.flag), zap.String("execute_sql", sql), zap.Int64("affect_rows", affectRows)) + dataList = append(dataList, zap.String("db_node", g.node), zap.String("execute_sql", sql), zap.Int64("affect_rows", affectRows)) message := "SQL执行记录" switch strings.ToLower(level) { case "info":