增加包装的DB Client
This commit is contained in:
parent
9daa1ba251
commit
0faf50ceba
@ -12,6 +12,8 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
|
|
||||||
"go.uber.org/zap/zapcore"
|
"go.uber.org/zap/zapcore"
|
||||||
@ -47,6 +49,22 @@ func NewGormV2(loggerLevel zapcore.Level, consoleOutput bool, encoder zapcore.En
|
|||||||
}, nil
|
}, 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 版本库日志实现
|
// Gorm v2 版本库日志实现
|
||||||
//
|
//
|
||||||
// Author : go_developer@163.com<白茶清欢>
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
@ -55,7 +73,10 @@ func NewGormV2(loggerLevel zapcore.Level, consoleOutput bool, encoder zapcore.En
|
|||||||
type Gorm struct {
|
type Gorm struct {
|
||||||
instance *zap.Logger // 日志实例
|
instance *zap.Logger // 日志实例
|
||||||
traceIDField string // 串联请求上下文的的ID
|
traceIDField string // 串联请求上下文的的ID
|
||||||
|
extraCtxFieldList []string // 从请求上线问提取的字段
|
||||||
flag string // 数据库标识
|
flag string // 数据库标识
|
||||||
|
node string // 数据库节点 master / slave
|
||||||
|
ctx *gin.Context // gin上下文
|
||||||
}
|
}
|
||||||
|
|
||||||
// LogMode ...
|
// LogMode ...
|
||||||
|
@ -10,14 +10,107 @@ package mysql
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"git.zhangdeman.cn/zhangdeman/gopkg/logger"
|
|
||||||
|
|
||||||
"git.zhangdeman.cn/zhangdeman/gopkg/logger/wrapper"
|
"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/driver/mysql"
|
||||||
"gorm.io/gorm"
|
"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 获取日志实例
|
// GetDatabaseClient 获取日志实例
|
||||||
//
|
//
|
||||||
// Author : go_developer@163.com<白茶清欢>
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
@ -27,33 +120,20 @@ func GetDatabaseClient(conf *DBConfig, logConf *LogConfig) (*gorm.DB, error) {
|
|||||||
var (
|
var (
|
||||||
instance *gorm.DB
|
instance *gorm.DB
|
||||||
err error
|
err error
|
||||||
|
loggerInstance *zap.Logger
|
||||||
)
|
)
|
||||||
|
|
||||||
if instance, err = gorm.Open(mysql.Open(buildConnectionDSN(conf)), &gorm.Config{}); nil != err {
|
if instance, err = gorm.Open(mysql.Open(buildConnectionDSN(conf)), &gorm.Config{}); nil != err {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(logConf.TraceFieldName) == 0 {
|
if nil != logConf {
|
||||||
logConf.TraceFieldName = defaultTraceFieldName
|
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
|
return instance, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user