From 7a5a8bf05756dedb69475071dd3b17d84f2730ac Mon Sep 17 00:00:00 2001 From: guo-shaoge Date: Wed, 8 Dec 2021 12:57:56 +0800 Subject: [PATCH] logutil: add testcase for SlowQueryLogger.MaxDays/MaxSize/MaxBackups (#30316) --- util/logutil/log_test.go | 15 +++++++++++++++ util/logutil/slow_query_logger.go | 28 +++++++++++++++------------- 2 files changed, 30 insertions(+), 13 deletions(-) diff --git a/util/logutil/log_test.go b/util/logutil/log_test.go index 713c34de77ab5..69eb731dee8c6 100644 --- a/util/logutil/log_test.go +++ b/util/logutil/log_test.go @@ -126,6 +126,21 @@ func TestSlowQueryLoggerCreation(t *testing.T) { // level should be less than WarnLevel which is used by it to log slow query. require.NotEqual(t, conf.Level, prop.Level.String()) require.True(t, prop.Level.Level() <= zapcore.WarnLevel) + + level = "warn" + slowQueryFn := "slow-query.log" + fileConf := FileLogConfig{ + log.FileLogConfig{ + Filename: slowQueryFn, + MaxSize: 10, + MaxDays: 10, + MaxBackups: 10, + }, + } + conf = NewLogConfig(level, DefaultLogFormat, slowQueryFn, fileConf, false) + slowQueryConf := newSlowQueryLogConfig(conf) + // slowQueryConf.MaxDays/MaxSize/MaxBackups should be same with global config. + require.Equal(t, fileConf.FileLogConfig, slowQueryConf.File) } func TestGlobalLoggerReplace(t *testing.T) { diff --git a/util/logutil/slow_query_logger.go b/util/logutil/slow_query_logger.go index 0c7fd10982314..d5d59f98aface 100644 --- a/util/logutil/slow_query_logger.go +++ b/util/logutil/slow_query_logger.go @@ -28,20 +28,8 @@ import ( var _pool = buffer.NewPool() func newSlowQueryLogger(cfg *LogConfig) (*zap.Logger, *log.ZapProperties, error) { - - // copy the global log config to slow log config - // if the filename of slow log config is empty, slow log will behave the same as global log. - sqConfig := cfg.Config - // level of the global log config doesn't affect the slow query logger which determines whether to - // log by execution duration. - sqConfig.Level = LogConfig{}.Level - if len(cfg.SlowQueryFile) != 0 { - sqConfig.File = cfg.File - sqConfig.File.Filename = cfg.SlowQueryFile - } - // create the slow query logger - sqLogger, prop, err := log.InitLogger(&sqConfig) + sqLogger, prop, err := log.InitLogger(newSlowQueryLogConfig(cfg)) if err != nil { return nil, nil, errors.Trace(err) } @@ -56,6 +44,20 @@ func newSlowQueryLogger(cfg *LogConfig) (*zap.Logger, *log.ZapProperties, error) return sqLogger, prop, nil } +func newSlowQueryLogConfig(cfg *LogConfig) *log.Config { + // copy the global log config to slow log config + // if the filename of slow log config is empty, slow log will behave the same as global log. + sqConfig := cfg.Config + // level of the global log config doesn't affect the slow query logger which determines whether to + // log by execution duration. + sqConfig.Level = LogConfig{}.Level + if len(cfg.SlowQueryFile) != 0 { + sqConfig.File = cfg.File + sqConfig.File.Filename = cfg.SlowQueryFile + } + return &sqConfig +} + type slowLogEncoder struct{} func (e *slowLogEncoder) EncodeEntry(entry zapcore.Entry, _ []zapcore.Field) (*buffer.Buffer, error) {