Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

logutil: add testcase for SlowQueryLogger.MaxDays/MaxSize/MaxBackups #30316

Merged
merged 8 commits into from
Dec 8, 2021
15 changes: 15 additions & 0 deletions util/logutil/log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
28 changes: 15 additions & 13 deletions util/logutil/slow_query_logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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) {
Expand Down