76 lines
1.7 KiB
Go
76 lines
1.7 KiB
Go
// Package mysql ...
|
|
//
|
|
// Description : mysql客户端
|
|
//
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
//
|
|
// Date : 2021-03-01 9:20 下午
|
|
package mysql
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"git.zhangdeman.cn/zhangdeman/gopkg/logger"
|
|
|
|
"git.zhangdeman.cn/zhangdeman/gopkg/logger/wrapper"
|
|
|
|
"gorm.io/driver/mysql"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// GetDatabaseClient 获取日志实例
|
|
//
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
//
|
|
// Date : 10:49 下午 2021/3/1
|
|
func GetDatabaseClient(conf *DBConfig, logConf *LogConfig) (*gorm.DB, error) {
|
|
var (
|
|
instance *gorm.DB
|
|
err error
|
|
)
|
|
|
|
if instance, err = gorm.Open(mysql.Open(buildConnectionDSN(conf)), &gorm.Config{}); nil != err {
|
|
return nil, err
|
|
}
|
|
|
|
if len(logConf.TraceFieldName) == 0 {
|
|
logConf.TraceFieldName = defaultTraceFieldName
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
// buildConnectionDSN 构建建立连接的DSN
|
|
//
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
//
|
|
// Date : 9:34 下午 2021/3/1
|
|
func buildConnectionDSN(conf *DBConfig) string {
|
|
return fmt.Sprintf(
|
|
"%s:%s@tcp(%s:%d)/%s?charset=%s&parseTime=True&loc=Local",
|
|
conf.Username,
|
|
conf.Password,
|
|
conf.Host,
|
|
conf.Port,
|
|
conf.Database,
|
|
conf.Charset,
|
|
)
|
|
}
|