修复sql日志记录的BUG

This commit is contained in:
白茶清欢 2023-10-08 21:35:08 +08:00
parent 785c9a2e48
commit da9b210104
3 changed files with 4 additions and 5 deletions

2
go.mod
View File

@ -6,7 +6,7 @@ require (
github.com/gin-gonic/gin v1.9.1
github.com/lestrrat-go/file-rotatelogs v2.4.0+incompatible
github.com/pkg/errors v0.9.1
go.uber.org/zap v1.25.0
go.uber.org/zap v1.26.0
gorm.io/gorm v1.25.3
)

2
go.sum
View File

@ -88,6 +88,8 @@ 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=
golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=
golang.org/x/arch v0.4.0 h1:A8WCeEWhLwPBKNbFi5Wv5UTCBx5zzubnXDlMOFAzFMc=
golang.org/x/arch v0.4.0/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=

View File

@ -141,6 +141,7 @@ func (g *Gorm) Trace(ctx context.Context, begin time.Time, fc func() (string, in
dataList := []zap.Field{
zap.String("db_flag", g.flag),
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)),
@ -176,10 +177,6 @@ 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("execute_sql", sql), zap.Int64("affect_rows", affectRows))
message := "SQL执行记录"
switch strings.ToLower(level) {
case "info":