修复日志BUG

This commit is contained in:
白茶清欢 2023-08-26 13:07:08 +08:00
parent 43ebfc11aa
commit 4566a456d9
3 changed files with 9 additions and 4 deletions

1
go.mod
View File

@ -11,6 +11,7 @@ require (
) )
require ( require (
git.zhangdeman.cn/zhangdeman/gin v0.0.0-20230811072113-817a4a831014 // indirect
github.com/bytedance/sonic v1.10.0 // indirect github.com/bytedance/sonic v1.10.0 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d // indirect github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d // indirect
github.com/chenzhuoyu/iasm v0.9.0 // indirect github.com/chenzhuoyu/iasm v0.9.0 // indirect

2
go.sum
View File

@ -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/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.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-rc/go.mod h1:ElCzW+ufi8qKqNW0FY314xriJhyJhuoJ3gFZdAHF7NM=

View File

@ -21,6 +21,7 @@ import (
"go.uber.org/zap" "go.uber.org/zap"
pkgGinDefine "git.zhangdeman.cn/zhangdeman/gin/define"
logger2 "git.zhangdeman.cn/zhangdeman/logger" logger2 "git.zhangdeman.cn/zhangdeman/logger"
"gorm.io/gorm/logger" "gorm.io/gorm/logger"
) )
@ -60,7 +61,7 @@ func NewGormLoggerWithInstance(ctx *gin.Context, dbClient *gorm.DB, instance *za
i := &Gorm{ i := &Gorm{
dbClient: dbClient, dbClient: dbClient,
instance: instance, instance: instance,
traceIDField: "", traceIDField: pkgGinDefine.TraceIDField,
extraCtxFieldList: extraCtxFieldList, extraCtxFieldList: extraCtxFieldList,
flag: "", flag: "",
node: node, node: node,
@ -139,7 +140,6 @@ func (g *Gorm) Trace(ctx context.Context, begin time.Time, fc func() (string, in
} }
dataList := []zap.Field{ dataList := []zap.Field{
zap.String(g.traceIDField, g.getTraceID(ctx)),
zap.String("db_flag", g.flag), zap.String("db_flag", g.flag),
zap.Int64("begin_time", start), zap.Int64("begin_time", start),
zap.Int64("finish_time", end), 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.Int64("affect_rows", affectRows),
zap.Error(err), zap.Error(err),
} }
if len(g.traceIDField) > 0 {
dataList = append(dataList, zap.String(g.traceIDField, g.getTraceID(ctx)))
}
g.write(dataList, "info") g.write(dataList, "info")
} }
// write ... // write ...
@ -177,7 +179,7 @@ func (g *Gorm) write(dataList []zap.Field, level string) {
// 补齐 flag、node // 补齐 flag、node
sql := g.dbClient.Dialector.Explain(g.dbClient.Statement.SQL.String(), g.dbClient.Statement.Vars...) sql := g.dbClient.Dialector.Explain(g.dbClient.Statement.SQL.String(), g.dbClient.Statement.Vars...)
affectRows := g.dbClient.RowsAffected 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执行记录" message := "SQL执行记录"
switch strings.ToLower(level) { switch strings.ToLower(level) {
case "info": case "info":