From f0cfac27b62d5770f0e9996387cdf4375fea49e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Sun, 15 Aug 2021 01:07:02 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dmysql=E6=97=A5=E5=BF=97?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- logger/wrapper/gorm_v2.go | 2 ++ middleware/mysql/client.go | 12 +++++++++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/logger/wrapper/gorm_v2.go b/logger/wrapper/gorm_v2.go index aea41d6..0a767f4 100644 --- a/logger/wrapper/gorm_v2.go +++ b/logger/wrapper/gorm_v2.go @@ -32,6 +32,7 @@ func NewGormV2(loggerLevel zapcore.Level, consoleOutput bool, encoder zapcore.En if consoleOutput { logConfList = append(logConfList, logger2.WithConsoleOutput()) } + logInstance, err := logger2.NewLogger(loggerLevel, splitConfig, logConfList...) if nil != err { return nil, err @@ -39,6 +40,7 @@ func NewGormV2(loggerLevel zapcore.Level, consoleOutput bool, encoder zapcore.En if len(traceIDField) == 0 { traceIDField = "trace_id" } + return &Gorm{ instance: logInstance, traceIDField: traceIDField, diff --git a/middleware/mysql/client.go b/middleware/mysql/client.go index ce286e0..86ff69d 100644 --- a/middleware/mysql/client.go +++ b/middleware/mysql/client.go @@ -10,6 +10,8 @@ package mysql import ( "fmt" + "git.zhangdeman.cn/zhangdeman/gopkg/logger" + "git.zhangdeman.cn/zhangdeman/gopkg/logger/wrapper" "gorm.io/driver/mysql" @@ -35,11 +37,19 @@ func GetDatabaseClient(conf *DBConfig, logConf *LogConfig) (*gorm.DB, error) { 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, - logConf.SplitConfig, + splitConfig, logConf.TraceFieldName, logConf.Skip); nil != err { return nil, CreateDBLogError(err)