Skip to content

Commit

Permalink
support compress (pingcap#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
magicedy committed Feb 26, 2024
1 parent 52eaa86 commit 3abf145
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
3 changes: 3 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ type FileLogConfig struct {
MaxDays int `toml:"max-days" json:"max-days"`
// Maximum number of old log files to retain.
MaxBackups int `toml:"max-backups" json:"max-backups"`
// Compress function for rotated files.
// Currently only `gzip` and empty are supported, empty means compression disabled.
Compress string `toml:"compress" json:"compress"`
}

// Config serializes log related config in toml/json.
Expand Down
11 changes: 11 additions & 0 deletions log.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,13 +141,24 @@ func initFileLog(cfg *FileLogConfig) (*lumberjack.Logger, error) {
cfg.MaxSize = defaultLogMaxSize
}

compress := false
switch cfg.Compress {
case "":
compress = false
case "gzip":
compress = true
default:
return nil, fmt.Errorf("can't set compress to `%s`", cfg.Compress)
}

// use lumberjack to logrotate
return &lumberjack.Logger{
Filename: logPath,
MaxSize: cfg.MaxSize,
MaxBackups: cfg.MaxBackups,
MaxAge: cfg.MaxDays,
LocalTime: true,
Compress: compress,
}, nil
}

Expand Down

0 comments on commit 3abf145

Please sign in to comment.