feature/mysql #11
@ -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
|
||||
|
@ -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
|
||||
|
@ -42,8 +42,3 @@ type LogConfig struct {
|
||||
TraceFieldName string
|
||||
Skip int
|
||||
}
|
||||
|
||||
const (
|
||||
// defaultTraceFieldName 默认trace_id字段
|
||||
defaultTraceFieldName = "trace_id"
|
||||
)
|
||||
|
Loading…
Reference in New Issue
Block a user