From 1a678f98082368918f62e6e395287a41c5923710 Mon Sep 17 00:00:00 2001 From: buddh0 Date: Wed, 13 Dec 2023 19:33:41 +0800 Subject: [PATCH] log: support to disable log rotate by hours --- log/async_file_writer.go | 15 +++++++++------ log/async_file_writer_test.go | 4 ++-- log/handler.go | 2 +- log/logger.go | 2 +- node/config.go | 2 +- node/node.go | 14 +++++++------- 6 files changed, 21 insertions(+), 18 deletions(-) diff --git a/log/async_file_writer.go b/log/async_file_writer.go index 2f37ab9c56..b4e7a60f30 100644 --- a/log/async_file_writer.go +++ b/log/async_file_writer.go @@ -17,15 +17,18 @@ type TimeTicker struct { // NewTimeTicker creates a TimeTicker that notifies based on rotateHours parameter. // if rotateHours is 1 and current time is 11:32 it means that the ticker will tick at 12:00 -// if rotateHours is 5 and current time is 09:12 means that the ticker will tick at 11:00 -func NewTimeTicker(rotateHours int) *TimeTicker { +// if rotateHours is 2 and current time is 09:12 means that the ticker will tick at 11:00 +// specially, if rotateHours is 0, then no rotation +func NewTimeTicker(rotateHours uint) *TimeTicker { ch := make(chan time.Time) tt := TimeTicker{ stop: make(chan struct{}), C: ch, } - tt.startTicker(ch, rotateHours) + if rotateHours > 0 { + tt.startTicker(ch, rotateHours) + } return &tt } @@ -34,7 +37,7 @@ func (tt *TimeTicker) Stop() { tt.stop <- struct{}{} } -func (tt *TimeTicker) startTicker(ch chan time.Time, rotateHours int) { +func (tt *TimeTicker) startTicker(ch chan time.Time, rotateHours uint) { go func() { nextRotationHour := getNextRotationHour(time.Now(), rotateHours) ticker := time.NewTicker(time.Second) @@ -53,7 +56,7 @@ func (tt *TimeTicker) startTicker(ch chan time.Time, rotateHours int) { }() } -func getNextRotationHour(now time.Time, delta int) int { +func getNextRotationHour(now time.Time, delta uint) int { return now.Add(time.Hour * time.Duration(delta)).Hour() } @@ -68,7 +71,7 @@ type AsyncFileWriter struct { timeTicker *TimeTicker } -func NewAsyncFileWriter(filePath string, maxBytesSize int64, rotateHours int) *AsyncFileWriter { +func NewAsyncFileWriter(filePath string, maxBytesSize int64, rotateHours uint) *AsyncFileWriter { absFilePath, err := filepath.Abs(filePath) if err != nil { panic(fmt.Sprintf("get file path of logger error. filePath=%s, err=%s", filePath, err)) diff --git a/log/async_file_writer_test.go b/log/async_file_writer_test.go index d6eae04567..ab12808856 100644 --- a/log/async_file_writer_test.go +++ b/log/async_file_writer_test.go @@ -29,7 +29,7 @@ func TestWriterHourly(t *testing.T) { func TestGetNextRotationHour(t *testing.T) { tcs := []struct { now time.Time - delta int + delta uint expectedHour int }{ { @@ -54,7 +54,7 @@ func TestGetNextRotationHour(t *testing.T) { }, } - test := func(now time.Time, delta, expectedHour int) func(*testing.T) { + test := func(now time.Time, delta uint, expectedHour int) func(*testing.T) { return func(t *testing.T) { got := getNextRotationHour(now, delta) if got != expectedHour { diff --git a/log/handler.go b/log/handler.go index 85413d06bf..bc407857f6 100644 --- a/log/handler.go +++ b/log/handler.go @@ -75,7 +75,7 @@ func FileHandler(path string, fmtr Format) (Handler, error) { // RotatingFileHandler returns a handler which writes log records to file chunks // at the given path. When a file's size reaches the limit, the handler creates // a new file named after the timestamp of the first log record it will contain. -func RotatingFileHandler(filePath string, limit uint, formatter Format, rotateHours int) (Handler, error) { +func RotatingFileHandler(filePath string, limit uint, formatter Format, rotateHours uint) (Handler, error) { if _, err := os.Stat(path.Dir(filePath)); os.IsNotExist(err) { err := os.MkdirAll(path.Dir(filePath), 0755) if err != nil { diff --git a/log/logger.go b/log/logger.go index bafc132899..5b89e699ec 100644 --- a/log/logger.go +++ b/log/logger.go @@ -290,7 +290,7 @@ func (c Ctx) toArray() []interface{} { return arr } -func NewFileLvlHandler(logPath string, maxBytesSize uint, level string, rotateHours int) Handler { +func NewFileLvlHandler(logPath string, maxBytesSize uint, level string, rotateHours uint) Handler { rfh, err := RotatingFileHandler(logPath, maxBytesSize, LogfmtFormat(), rotateHours) if err != nil { panic(err) diff --git a/node/config.go b/node/config.go index 5ccb47474a..dc27d48a58 100644 --- a/node/config.go +++ b/node/config.go @@ -512,7 +512,7 @@ type LogConfig struct { FilePath *string `toml:",omitempty"` MaxBytesSize *uint `toml:",omitempty"` Level *string `toml:",omitempty"` - RotateHours int `toml:",omitempty"` + RotateHours *uint `toml:",omitempty"` // TermTimeFormat is the time format used for console logging. TermTimeFormat *string `toml:",omitempty"` diff --git a/node/node.go b/node/node.go index aca3231e77..81b2306200 100644 --- a/node/node.go +++ b/node/node.go @@ -109,16 +109,16 @@ func New(conf *Config) (*Node, error) { logFilePath = path.Join(*conf.LogConfig.FileRoot, *conf.LogConfig.FilePath) } - if conf.LogConfig.RotateHours > 24 { - return nil, errors.New("Config.LogConfig.RotateHours cannot be greater than 24") - } + rotateHours := uint(1) // To maintain backwards compatibility, if RotateHours is not set, then it defaults to 1 + if conf.LogConfig.RotateHours != nil { + if *conf.LogConfig.RotateHours > 24 { + return nil, errors.New("Config.LogConfig.RotateHours cannot be greater than 24") + } - // To maintain backwards compatibility, if RotateHours is not set or set to a negative value, then it defaults to 1 - if conf.LogConfig.RotateHours < 1 { - conf.LogConfig.RotateHours = 1 + rotateHours = *conf.LogConfig.RotateHours } - log.Root().SetHandler(log.NewFileLvlHandler(logFilePath, *conf.LogConfig.MaxBytesSize, *conf.LogConfig.Level, conf.LogConfig.RotateHours)) + log.Root().SetHandler(log.NewFileLvlHandler(logFilePath, *conf.LogConfig.MaxBytesSize, *conf.LogConfig.Level, rotateHours)) } } if conf.Logger == nil {