From 28681affdafef819f83671c426d8cb09bcaa09cc Mon Sep 17 00:00:00 2001 From: Svarog Date: Thu, 14 May 2020 10:58:46 +0300 Subject: [PATCH] [NOD-994] Greatly increase the amount of logs kaspad keeps before rotating them away (#720) * [NOD-994] Greatly increased the amount of logs kaspad keeps before rotating them away * [NOD-994] Actually invcrease the log file * [NOD-994] Update comments * [NOD-994] Fix typo --- logger/logger.go | 2 +- logs/logs.go | 19 ++++++++++++++++--- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/logger/logger.go b/logger/logger.go index 06fe2431a..a53b6f264 100644 --- a/logger/logger.go +++ b/logger/logger.go @@ -113,7 +113,7 @@ var subsystemLoggers = map[string]*logs.Logger{ // InitLog attaches log file and error log file to the backend log. func InitLog(logFile, errLogFile string) { - err := BackendLog.AddLogFile(logFile, logs.LevelTrace) + err := BackendLog.AddLogFileWithCustomRotator(logFile, logs.LevelTrace, 100*1024, 4) if err != nil { fmt.Fprintf(os.Stderr, "Error adding log file %s as log rotator for level %s: %s", logFile, logs.LevelTrace, err) os.Exit(1) diff --git a/logs/logs.go b/logs/logs.go index 7984031b3..e42f53022 100644 --- a/logs/logs.go +++ b/logs/logs.go @@ -35,7 +35,6 @@ package logs import ( "bytes" "fmt" - "github.com/pkg/errors" "os" "path/filepath" "runtime" @@ -44,6 +43,8 @@ import ( "sync/atomic" "time" + "github.com/pkg/errors" + "github.com/jrick/logrotate/rotator" ) @@ -265,15 +266,27 @@ func callsite(flag uint32) (string, int) { return file, line } +const ( + defaultThresholdKB = 10 * 1024 + defaultMaxRolls = 3 +) + // AddLogFile adds a file which the log will write into on a certain -// log level. It'll create the file if it doesn't exist. +// log level with the default log rotation settings. It'll create the file if it doesn't exist. func (b *Backend) AddLogFile(logFile string, logLevel Level) error { + return b.AddLogFileWithCustomRotator(logFile, logLevel, defaultThresholdKB, defaultMaxRolls) +} + +// AddLogFileWithCustomRotator adds a file which the log will write into on a certain +// log level, with the specified log rotation settings. +// It'll create the file if it doesn't exist. +func (b *Backend) AddLogFileWithCustomRotator(logFile string, logLevel Level, thresholdKB int64, maxRolls int) error { logDir, _ := filepath.Split(logFile) err := os.MkdirAll(logDir, 0700) if err != nil { return errors.Errorf("failed to create log directory: %s", err) } - r, err := rotator.New(logFile, 10*1024, false, 3) + r, err := rotator.New(logFile, thresholdKB, false, maxRolls) if err != nil { return errors.Errorf("failed to create file rotator: %s", err) }