Skip to content

Commit

Permalink
logutil: add testcase for SlowQueryLogger.MaxDays/MaxSize/MaxBackups (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
guo-shaoge authored Dec 8, 2021
1 parent 27f7b59 commit 7a5a8bf
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 13 deletions.
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

0 comments on commit 7a5a8bf

Please sign in to comment.