From ff1f61bf694b9d3aa2ed0156758f85209683ca5a Mon Sep 17 00:00:00 2001 From: SliverHorn Date: Fri, 9 Jul 2021 21:52:17 +0800 Subject: [PATCH] =?UTF-8?q?update:=20=E8=87=AA=E5=AE=9A=E4=B9=89gorm?= =?UTF-8?q?=E7=9A=84logger=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/config/gorm.go | 4 +- server/initialize/gorm.go | 16 ++----- server/initialize/internal/logger.go | 72 ++++++++-------------------- server/initialize/timer.go | 1 - 4 files changed, 28 insertions(+), 65 deletions(-) diff --git a/server/config/gorm.go b/server/config/gorm.go index 2524b190..ae6c7384 100644 --- a/server/config/gorm.go +++ b/server/config/gorm.go @@ -8,8 +8,8 @@ type Mysql struct { Password string `mapstructure:"password" json:"password" yaml:"password"` // 数据库密码 MaxIdleConns int `mapstructure:"max-idle-conns" json:"maxIdleConns" yaml:"max-idle-conns"` // 空闲中的最大连接数 MaxOpenConns int `mapstructure:"max-open-conns" json:"maxOpenConns" yaml:"max-open-conns"` // 打开到数据库的最大连接数 - LogMode bool `mapstructure:"log-mode" json:"logMode" yaml:"log-mode"` // 是否开启Gorm全局日志 - LogZap string `mapstructure:"log-zap" json:"logZap" yaml:"log-zap"` + LogMode string `mapstructure:"log-mode" json:"logMode" yaml:"log-mode"` // 是否开启Gorm全局日志 + LogZap bool `mapstructure:"log-zap" json:"logZap" yaml:"log-zap"` // 是否通过zap写入日志文件 } func (m *Mysql) Dsn() string { diff --git a/server/initialize/gorm.go b/server/initialize/gorm.go index 0ac364c9..c3d997c8 100644 --- a/server/initialize/gorm.go +++ b/server/initialize/gorm.go @@ -78,7 +78,7 @@ func GormMysql() *gorm.DB { DontSupportRenameColumn: true, // 用 `change` 重命名列,MySQL 8 之前的数据库和 MariaDB 不支持重命名列 SkipInitializeWithVersion: false, // 根据版本自动配置 } - if db, err := gorm.Open(mysql.New(mysqlConfig), gormConfig(m.LogMode)); err != nil { + if db, err := gorm.Open(mysql.New(mysqlConfig), gormConfig()); err != nil { //global.GVA_LOG.Error("MySQL启动异常", zap.Any("err", err)) //os.Exit(0) //return nil @@ -97,9 +97,9 @@ func GormMysql() *gorm.DB { //@param: mod bool //@return: *gorm.Config -func gormConfig(mod bool) *gorm.Config { - var config = &gorm.Config{DisableForeignKeyConstraintWhenMigrating: true} - switch global.GVA_CONFIG.Mysql.LogZap { +func gormConfig() *gorm.Config { + config := &gorm.Config{DisableForeignKeyConstraintWhenMigrating: true} + switch global.GVA_CONFIG.Mysql.LogMode { case "silent", "Silent": config.Logger = internal.Default.LogMode(logger.Silent) case "error", "Error": @@ -108,14 +108,8 @@ func gormConfig(mod bool) *gorm.Config { config.Logger = internal.Default.LogMode(logger.Warn) case "info", "Info": config.Logger = internal.Default.LogMode(logger.Info) - case "zap", "Zap": - config.Logger = internal.Default.LogMode(logger.Info) default: - if mod { - config.Logger = internal.Default.LogMode(logger.Info) - break - } - config.Logger = internal.Default.LogMode(logger.Silent) + config.Logger = internal.Default.LogMode(logger.Info) } return config } diff --git a/server/initialize/internal/logger.go b/server/initialize/internal/logger.go index e44f563c..c7fe9254 100644 --- a/server/initialize/internal/logger.go +++ b/server/initialize/internal/logger.go @@ -4,7 +4,6 @@ import ( "context" "fmt" "gin-vue-admin/global" - "go.uber.org/zap" "gorm.io/gorm/logger" "gorm.io/gorm/utils" "io/ioutil" @@ -13,11 +12,6 @@ import ( "time" ) -// writer log writer interface -type writer interface { - Printf(string, ...interface{}) -} - type config struct { SlowThreshold time.Duration Colorful bool @@ -34,27 +28,27 @@ var ( Recorder = traceRecorder{Interface: Default, BeginAt: time.Now()} ) -func New(writer writer, config config) logger.Interface { +func New(writer logger.Writer, config config) logger.Interface { var ( infoStr = "%s\n[info] " warnStr = "%s\n[warn] " errStr = "%s\n[error] " - traceStr = "%s\n[%.3fms] [rows:%v] %s" - traceWarnStr = "%s %s\n[%.3fms] [rows:%v] %s" - traceErrStr = "%s %s\n[%.3fms] [rows:%v] %s" + traceStr = "%s\n[%.3fms] [rows:%v] %s\n" + traceWarnStr = "%s %s\n[%.3fms] [rows:%v] %s\n" + traceErrStr = "%s %s\n[%.3fms] [rows:%v] %s\n" ) if config.Colorful { infoStr = logger.Green + "%s\n" + logger.Reset + logger.Green + "[info] " + logger.Reset warnStr = logger.BlueBold + "%s\n" + logger.Reset + logger.Magenta + "[warn] " + logger.Reset errStr = logger.Magenta + "%s\n" + logger.Reset + logger.Red + "[error] " + logger.Reset - traceStr = logger.Green + "%s\n" + logger.Reset + logger.Yellow + "[%.3fms] " + logger.BlueBold + "[rows:%v]" + logger.Reset + " %s" - traceWarnStr = logger.Green + "%s " + logger.Yellow + "%s\n" + logger.Reset + logger.RedBold + "[%.3fms] " + logger.Yellow + "[rows:%v]" + logger.Magenta + " %s" + logger.Reset - traceErrStr = logger.RedBold + "%s " + logger.MagentaBold + "%s\n" + logger.Reset + logger.Yellow + "[%.3fms] " + logger.BlueBold + "[rows:%v]" + logger.Reset + " %s" + traceStr = logger.Green + "%s\n" + logger.Reset + logger.Yellow + "[%.3fms] " + logger.BlueBold + "[rows:%v]" + logger.Reset + " %s\n" + traceWarnStr = logger.Green + "%s " + logger.Yellow + "%s\n" + logger.Reset + logger.RedBold + "[%.3fms] " + logger.Yellow + "[rows:%v]" + logger.Magenta + " %s\n" + logger.Reset + traceErrStr = logger.RedBold + "%s " + logger.MagentaBold + "%s\n" + logger.Reset + logger.Yellow + "[%.3fms] " + logger.BlueBold + "[rows:%v]" + logger.Reset + " %s\n" } - return &customLogger{ - writer: writer, + return &_logger{ + Writer: writer, config: config, infoStr: infoStr, warnStr: warnStr, @@ -65,43 +59,43 @@ func New(writer writer, config config) logger.Interface { } } -type customLogger struct { - writer +type _logger struct { config + logger.Writer infoStr, warnStr, errStr string traceStr, traceErrStr, traceWarnStr string } // LogMode log mode -func (c *customLogger) LogMode(level logger.LogLevel) logger.Interface { +func (c *_logger) LogMode(level logger.LogLevel) logger.Interface { newLogger := *c newLogger.LogLevel = level return &newLogger } // Info print info -func (c *customLogger) Info(ctx context.Context, message string, data ...interface{}) { +func (c *_logger) Info(ctx context.Context, message string, data ...interface{}) { if c.LogLevel >= logger.Info { c.Printf(c.infoStr+message, append([]interface{}{utils.FileWithLineNum()}, data...)...) } } // Warn print warn messages -func (c *customLogger) Warn(ctx context.Context, message string, data ...interface{}) { +func (c *_logger) Warn(ctx context.Context, message string, data ...interface{}) { if c.LogLevel >= logger.Warn { c.Printf(c.warnStr+message, append([]interface{}{utils.FileWithLineNum()}, data...)...) } } // Error print error messages -func (c *customLogger) Error(ctx context.Context, message string, data ...interface{}) { +func (c *_logger) Error(ctx context.Context, message string, data ...interface{}) { if c.LogLevel >= logger.Error { c.Printf(c.errStr+message, append([]interface{}{utils.FileWithLineNum()}, data...)...) } } // Trace print sql message -func (c *customLogger) Trace(ctx context.Context, begin time.Time, fc func() (string, int64), err error) { +func (c *_logger) Trace(ctx context.Context, begin time.Time, fc func() (string, int64), err error) { if c.LogLevel > 0 { elapsed := time.Since(begin) switch { @@ -131,35 +125,11 @@ func (c *customLogger) Trace(ctx context.Context, begin time.Time, fc func() (st } } -func (c *customLogger) Printf(message string, data ...interface{}) { - if global.GVA_CONFIG.Mysql.LogZap != "" { - switch len(data) { - case 0: - global.GVA_LOG.Info(message) - case 1: - global.GVA_LOG.Info("gorm", zap.Any("src", data[0])) - case 2: - global.GVA_LOG.Info("gorm", zap.Any("src", data[0]), zap.Any("duration", data[1])) - case 3: - global.GVA_LOG.Info("gorm", zap.Any("src", data[0]), zap.Any("duration", data[1]), zap.Any("rows", data[2])) - case 4: - global.GVA_LOG.Info("gorm", zap.Any("src", data[0]), zap.Any("duration", data[1]), zap.Any("rows", data[2]), zap.Any("sql", data[3])) - } - return - } - switch len(data) { - case 0: - c.writer.Printf(message, "") - case 1: - c.writer.Printf(message, data[0]) - case 2: - c.writer.Printf(message, data[0], data[1]) - case 3: - c.writer.Printf(message, data[0], data[1], data[2]) - case 4: - c.writer.Printf(message, data[0], data[1], data[2], data[3]) - case 5: - c.writer.Printf(message, data[0], data[1], data[2], data[3], data[4]) +func (c *_logger) Printf(message string, data ...interface{}) { + if global.GVA_CONFIG.Mysql.LogZap { + global.GVA_LOG.Info(fmt.Sprintf(message, data...)) + } else { + c.Writer.Printf(message, data...) } } diff --git a/server/initialize/timer.go b/server/initialize/timer.go index 6fa27913..ecc41c8d 100644 --- a/server/initialize/timer.go +++ b/server/initialize/timer.go @@ -10,7 +10,6 @@ import ( func Timer() { if global.GVA_CONFIG.Timer.Start { for _, detail := range global.GVA_CONFIG.Timer.Detail { - fmt.Println(detail) go func(detail config.Detail) { global.GVA_Timer.AddTaskByFunc("ClearDB", global.GVA_CONFIG.Timer.Spec, func() { err := utils.ClearTable(global.GVA_DB, detail.TableName, detail.CompareField, detail.Interval)