增加包装的DB Client

This commit is contained in:
白茶清欢 2021-12-24 16:00:48 +08:00
parent 9daa1ba251
commit 0faf50ceba
2 changed files with 128 additions and 27 deletions

View File

@ -12,6 +12,8 @@ import (
"fmt"
"time"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
"go.uber.org/zap/zapcore"
@ -47,15 +49,34 @@ func NewGormV2(loggerLevel zapcore.Level, consoleOutput bool, encoder zapcore.En
}, nil
}
// NewGormLoggerWithInstance 获取gorm日志实现
//
// 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 {
return &Gorm{
instance: instance,
traceIDField: "",
extraCtxFieldList: extraCtxFieldList,
flag: "",
node: node,
ctx: ctx,
}
}
// Gorm v2 版本库日志实现
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 9:55 下午 2021/3/1
type Gorm struct {
instance *zap.Logger // 日志实例
traceIDField string // 串联请求上下文的的ID
flag string // 数据库标识
instance *zap.Logger // 日志实例
traceIDField string // 串联请求上下文的的ID
extraCtxFieldList []string // 从请求上线问提取的字段
flag string // 数据库标识
node string // 数据库节点 master / slave
ctx *gin.Context // gin上下文
}
// LogMode ...
@ -145,7 +166,7 @@ func (g *Gorm) getTraceID(ctx context.Context) string {
return fmt.Sprintf("%v", ctx.Value(g.traceIDField))
}
// GetGormSQL 获取tracefn
// GetGormSQL 获取trace fn
//
// Author : go_developer@163.com<白茶清欢>
//

View File

@ -10,14 +10,107 @@ package mysql
import (
"fmt"
"git.zhangdeman.cn/zhangdeman/gopkg/logger"
"git.zhangdeman.cn/zhangdeman/gopkg/logger/wrapper"
"github.com/gin-gonic/gin"
"go.uber.org/zap"
"git.zhangdeman.cn/zhangdeman/gopkg/logger"
gormLogger "gorm.io/gorm/logger"
"gorm.io/driver/mysql"
"gorm.io/gorm"
)
// NewDBClient ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 3:09 PM 2021/12/24
func NewDBClient(masterConf *DBConfig, slaveConf *DBConfig, logConf *LogConfig, loggerInstance *zap.Logger, extraRequestFieldList []string) (*DBClient, error) {
client := &DBClient{
extraFieldList: extraRequestFieldList,
}
var err error
// 日志初始化失败
if client.loggerInstance, err = getLogInstance(logConf, loggerInstance); nil != err {
return nil, err
}
if client.master, err = GetDatabaseClient(masterConf, nil); nil != err {
return nil, err
}
if client.slave, err = GetDatabaseClient(slaveConf, nil); nil != err {
return nil, err
}
return client, nil
}
// DBClient 包装日志实例
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 3:09 PM 2021/12/24
type DBClient struct {
loggerInstance *zap.Logger
master *gorm.DB
slave *gorm.DB
extraFieldList []string
}
// GetMaster 获取主库连接
//
// Author : go_developer@163.com<白茶清欢>
//
// 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"),
})
}
// GetSlave 获取从库链接
//
// Author : go_developer@163.com<白茶清欢>
//
// 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"),
})
}
// getLogger 获取日志实例
//
// 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)
}
// getLogInstance 获取日志实例
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 3:20 PM 2021/12/24
func getLogInstance(logConf *LogConfig, loggerInstance *zap.Logger) (*zap.Logger, error) {
if nil != loggerInstance {
return loggerInstance, nil
}
logConfList := []logger.SetLoggerOptionFunc{logger.WithEncoder(logConf.Encoder), logger.WithCallerSkip(logConf.Skip), logger.WithCaller()}
if logConf.ConsoleOutput {
logConfList = append(logConfList, logger.WithConsoleOutput())
}
var (
err error
)
if loggerInstance, err = logger.NewLogger(logConf.Level, logConf.SplitConfig, logConfList...); nil != err {
return nil, err
}
return loggerInstance, nil
}
// GetDatabaseClient 获取日志实例
//
// Author : go_developer@163.com<白茶清欢>
@ -25,35 +118,22 @@ import (
// Date : 10:49 下午 2021/3/1
func GetDatabaseClient(conf *DBConfig, logConf *LogConfig) (*gorm.DB, error) {
var (
instance *gorm.DB
err error
instance *gorm.DB
err error
loggerInstance *zap.Logger
)
if instance, err = gorm.Open(mysql.Open(buildConnectionDSN(conf)), &gorm.Config{}); nil != err {
return nil, err
}
if len(logConf.TraceFieldName) == 0 {
logConf.TraceFieldName = defaultTraceFieldName
if nil != logConf {
if loggerInstance, err = getLogInstance(logConf, nil); nil != err {
return nil, err
}
instance.Logger = wrapper.NewGormLoggerWithInstance(nil, loggerInstance, "", nil)
}
splitConfigFuncList := []logger.SetRotateLogConfigFunc{
logger.WithTimeIntervalType(logConf.SplitConfig.TimeIntervalType),
logger.WithDivisionChar(logConf.SplitConfig.DivisionChar),
logger.WithMaxAge(logConf.SplitConfig.MaxAge),
}
splitConfig, _ := logger.NewRotateLogConfig(logConf.SplitConfig.LogPath, logConf.SplitConfig.LogFileName, splitConfigFuncList...)
if instance.Logger, err = wrapper.NewGormV2(
logConf.Level,
logConf.ConsoleOutput,
logConf.Encoder,
splitConfig,
logConf.TraceFieldName,
logConf.Skip); nil != err {
return nil, CreateDBLogError(err)
}
return instance, nil
}