From ce1321b5ebc088b1ed81ed85cee4df49c97c5b61 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Fri, 24 Dec 2021 16:28:09 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8D=87=E7=BA=A7mysql=E6=97=A5=E5=BF=97?= =?UTF-8?q?=E5=8C=85=E8=A3=85=E5=AE=9E=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- logger/wrapper/gorm_v2.go | 69 ++++++++++++++++++++++++++------------ middleware/mysql/client.go | 18 +++++----- middleware/mysql/define.go | 5 --- 3 files changed, 56 insertions(+), 36 deletions(-) diff --git a/logger/wrapper/gorm_v2.go b/logger/wrapper/gorm_v2.go index 889a023..9faae75 100644 --- a/logger/wrapper/gorm_v2.go +++ b/logger/wrapper/gorm_v2.go @@ -10,6 +10,7 @@ package wrapper import ( "context" "fmt" + "strings" "time" "github.com/gin-gonic/gin" @@ -54,8 +55,9 @@ 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, instance *zap.Logger, node string, extraCtxFieldList []string) logger.Interface { +func NewGormLoggerWithInstance(ctx *gin.Context, dbClient *gorm.DB, instance *zap.Logger, node string, extraCtxFieldList []string) logger.Interface { return &Gorm{ + dbClient: dbClient, instance: instance, traceIDField: "", extraCtxFieldList: extraCtxFieldList, @@ -71,6 +73,7 @@ func NewGormLoggerWithInstance(ctx *gin.Context, instance *zap.Logger, node stri // // Date : 9:55 下午 2021/3/1 type Gorm struct { + dbClient *gorm.DB instance *zap.Logger // 日志实例 traceIDField string // 串联请求上下文的的ID extraCtxFieldList []string // 从请求上线问提取的字段 @@ -94,12 +97,7 @@ func (g *Gorm) LogMode(level logger.LogLevel) logger.Interface { // // Date : 10:18 下午 2021/3/1 func (g *Gorm) Info(ctx context.Context, s string, i ...interface{}) { - g.instance.Info( - "Info日志", - zap.String(g.traceIDField, g.getTraceID(ctx)), - zap.String("db_flag", g.flag), - zap.String("message", fmt.Sprintf(s, i...)), - ) + g.write(nil, "info") } // Warn ... @@ -108,12 +106,7 @@ func (g *Gorm) Info(ctx context.Context, s string, i ...interface{}) { // // Date : 10:16 下午 2021/3/1 func (g *Gorm) Warn(ctx context.Context, s string, i ...interface{}) { - g.instance.Warn( - "SQL执行产生Warning", - zap.String(g.traceIDField, g.getTraceID(ctx)), - zap.String("db_flag", g.flag), - zap.String("message", fmt.Sprintf(s, i...)), - ) + g.write(nil, "warn") } // Error 日志 @@ -122,12 +115,7 @@ func (g *Gorm) Warn(ctx context.Context, s string, i ...interface{}) { // // Date : 10:18 下午 2021/3/1 func (g *Gorm) Error(ctx context.Context, s string, i ...interface{}) { - g.instance.Warn( - "SQL执行产生Error", - zap.String(g.traceIDField, g.getTraceID(ctx)), - zap.String("db_flag", g.flag), - zap.String("message", fmt.Sprintf(s, i...)), - ) + g.write(nil, "error") } // Trace Trace 记录 @@ -144,8 +132,7 @@ func (g *Gorm) Trace(ctx context.Context, begin time.Time, fc func() (string, in sql, affectRows = fc() } - g.instance.Info( - "SQL执行记录", + dataList := []zap.Field{ zap.String(g.traceIDField, g.getTraceID(ctx)), zap.String("db_flag", g.flag), zap.Int64("begin_time", start), @@ -154,7 +141,45 @@ func (g *Gorm) Trace(ctx context.Context, begin time.Time, fc func() (string, in zap.String("sql", sql), zap.Int64("affect_rows", affectRows), zap.Error(err), - ) + } + g.write(dataList, "info") + +} + +// write ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 4:11 PM 2021/12/24 +func (g *Gorm) write(dataList []zap.Field, level string) { + if nil == g.instance { + // 未设置日志实例 + return + } + if nil == dataList { + dataList = make([]zap.Field, 0) + } + if nil != g.ctx { + for _, extraField := range g.extraCtxFieldList { + dataList = append(dataList, zap.Any(extraField, g.ctx.Value(extraField))) + } + } + + // 补齐 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("affext_rows", affectRows)) + message := "SQL执行记录" + switch strings.ToLower(level) { + case "info": + g.instance.Info(message, dataList...) + case "warn": + g.instance.Warn(message, dataList...) + case "error": + g.instance.Error(message, dataList...) + default: + g.instance.Info(message, dataList...) + } } // getTraceID 获取traceID diff --git a/middleware/mysql/client.go b/middleware/mysql/client.go index aa52571..80a1838 100644 --- a/middleware/mysql/client.go +++ b/middleware/mysql/client.go @@ -63,9 +63,9 @@ type DBClient struct { // // Date : 3:28 PM 2021/12/24 func (dc *DBClient) GetMaster(ctx *gin.Context) *gorm.DB { - return dc.master.Session(&gorm.Session{ - Logger: dc.getLogger(ctx, "master"), - }) + session := dc.master.Session(&gorm.Session{}) + session.Logger = dc.getLogger(ctx, session, "slave") + return session } // GetSlave 获取从库链接 @@ -74,9 +74,9 @@ func (dc *DBClient) GetMaster(ctx *gin.Context) *gorm.DB { // // Date : 3:29 PM 2021/12/24 func (dc *DBClient) GetSlave(ctx *gin.Context) *gorm.DB { - return dc.slave.Session(&gorm.Session{ - Logger: dc.getLogger(ctx, "slave"), - }) + session := dc.slave.Session(&gorm.Session{}) + session.Logger = dc.getLogger(ctx, session, "slave") + return session } // getLogger 获取日志实例 @@ -84,8 +84,8 @@ func (dc *DBClient) GetSlave(ctx *gin.Context) *gorm.DB { // Author : go_developer@163.com<白茶清欢> // // Date : 3:45 PM 2021/12/24 -func (dc *DBClient) getLogger(ctx *gin.Context, node string) gormLogger.Interface { - return wrapper.NewGormLoggerWithInstance(ctx, dc.loggerInstance, node, dc.extraFieldList) +func (dc *DBClient) getLogger(ctx *gin.Context, dbClient *gorm.DB, node string) gormLogger.Interface { + return wrapper.NewGormLoggerWithInstance(ctx, dbClient, dc.loggerInstance, node, dc.extraFieldList) } // getLogInstance 获取日志实例 @@ -131,7 +131,7 @@ func GetDatabaseClient(conf *DBConfig, logConf *LogConfig) (*gorm.DB, error) { if loggerInstance, err = getLogInstance(logConf, nil); nil != err { return nil, err } - instance.Logger = wrapper.NewGormLoggerWithInstance(nil, loggerInstance, "", nil) + instance.Logger = wrapper.NewGormLoggerWithInstance(nil, instance, loggerInstance, "", nil) } return instance, nil diff --git a/middleware/mysql/define.go b/middleware/mysql/define.go index 8c21092..c45b1b7 100644 --- a/middleware/mysql/define.go +++ b/middleware/mysql/define.go @@ -42,8 +42,3 @@ type LogConfig struct { TraceFieldName string Skip int } - -const ( - // defaultTraceFieldName 默认trace_id字段 - defaultTraceFieldName = "trace_id" -)