feat: migrate logger impl

This commit is contained in:
2025-10-12 17:32:04 +08:00
parent cb3d32c360
commit c64e328ea4
3 changed files with 185 additions and 64 deletions

View File

@@ -11,24 +11,21 @@ import (
"context"
"errors"
"fmt"
"strings"
"sync"
"time"
"git.zhangdeman.cn/zhangdeman/consts"
"git.zhangdeman.cn/zhangdeman/database/define"
"git.zhangdeman.cn/zhangdeman/logger/wrapper"
"git.zhangdeman.cn/zhangdeman/database/logger"
"go.uber.org/zap"
"gorm.io/driver/mysql"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
gormLogger "gorm.io/gorm/logger"
"strings"
"sync"
"time"
)
// DBClient 包装日志实例
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 3:09 PM 2021/12/24
type DBClient struct {
DbFlag string // 数据库标识
LoggerInstance *zap.Logger // 日志实例
@@ -42,10 +39,6 @@ type DBClient struct {
}
// Init 初始化客户端
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 17:44 2024/8/20
func (dc *DBClient) Init(databaseConfig *define.Database, cacheTableStructureConfig *define.CacheTableStructureConfig) error {
dc.lock = &sync.RWMutex{}
dc.cacheTableStructureConfig = cacheTableStructureConfig
@@ -76,10 +69,6 @@ func (dc *DBClient) Init(databaseConfig *define.Database, cacheTableStructureCon
}
// GetMaster 获取主库连接
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 3:28 PM 2021/12/24
func (dc *DBClient) GetMaster(ctx context.Context) *gorm.DB {
session := dc.master.Session(&gorm.Session{})
session.Logger = dc.getLogger(ctx, session, "master")
@@ -87,10 +76,6 @@ func (dc *DBClient) GetMaster(ctx context.Context) *gorm.DB {
}
// GetSlave 获取从库链接
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 3:29 PM 2021/12/24
func (dc *DBClient) GetSlave(ctx context.Context) *gorm.DB {
session := dc.slave.Session(&gorm.Session{})
session.Logger = dc.getLogger(ctx, session, "slave")
@@ -98,12 +83,8 @@ func (dc *DBClient) GetSlave(ctx context.Context) *gorm.DB {
}
// getLogger 获取日志实例
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 3:45 PM 2021/12/24
func (dc *DBClient) getLogger(ctx context.Context, dbClient *gorm.DB, node string) gormLogger.Interface {
return wrapper.NewGormLoggerWithInstance(ctx, dbClient, dc.LoggerInstance, dc.DbFlag+"|"+node, dc.ExtraFieldList)
return logger.NewGormLoggerWithInstance(ctx, dbClient, dc.LoggerInstance, dc.DbFlag+"|"+node, dc.ExtraFieldList)
}
// GetDatabaseClient 获取数据库连接
@@ -142,16 +123,12 @@ func (dc *DBClient) GetDatabaseClient(conf *define.Driver, logInstance *zap.Logg
}
dbInstance.SetMaxIdleConns(conf.Connection.MaxIdle)
dbInstance.SetMaxOpenConns(conf.Connection.MaxOpen)
instance.Logger = wrapper.NewGormLoggerWithInstance(nil, instance, logInstance, "", nil)
instance.Logger = logger.NewGormLoggerWithInstance(nil, instance, logInstance, "", nil)
return instance, nil
}
// buildConnectionDSN 构建连接信息
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:42 2022/6/11
func (dc *DBClient) buildConnectionDSN(conf *define.Driver) string {
if conf.DBType == consts.DatabaseDriverSqlite3 {
// 兼容sqlite3
@@ -170,10 +147,6 @@ func (dc *DBClient) buildConnectionDSN(conf *define.Driver) string {
}
// CacheDataTableStructureConfig ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 15:03 2024/8/21
func (dc *DBClient) CacheDataTableStructureConfig() *define.CacheTableStructureConfig {
if nil == dc.cacheTableStructureConfig {
return &define.CacheTableStructureConfig{Enable: false, SyncTimeInterval: 3600}
@@ -185,10 +158,6 @@ func (dc *DBClient) CacheDataTableStructureConfig() *define.CacheTableStructureC
}
// GetTableFieldList 获取表结构配置
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 15:07 2024/8/21
func (dc *DBClient) GetTableFieldList(tableName string) ([]*define.ColumnConfig, error) {
if !dc.CacheDataTableStructureConfig().Enable {
// 未启用缓存, 返回空list
@@ -203,10 +172,6 @@ func (dc *DBClient) GetTableFieldList(tableName string) ([]*define.ColumnConfig,
}
// syncDbTableStructure 缓存表结构
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 15:17 2024/8/21
func (dc *DBClient) syncDbTableStructure(ignoreErr bool) error {
if !dc.CacheDataTableStructureConfig().Enable {
// 自动同步不可用
@@ -256,10 +221,6 @@ func (dc *DBClient) syncDbTableStructure(ignoreErr bool) error {
}
// SetTableStructure 设置表结构, 一旦调用人工设置, 则将终止自动同步
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 17:06 2024/8/23
func (dc *DBClient) SetTableStructure(tableConfigTable map[string][]*define.ColumnConfig) {
if nil != dc.cacheTableStructureConfig {
// 关闭自动同步