From b140dd61b5feffed979043c8fc87e43cd5e077d9 Mon Sep 17 00:00:00 2001 From: Daniel Nephin Date: Thu, 22 Oct 2020 14:31:51 -0400 Subject: [PATCH] logging: call pruneFiles on startup To ensure that files are pruned before a new one is created. Also clean up the logic in pruneFiles --- logging/logfile.go | 30 +++++++++++++++++------------- logging/logger.go | 3 +++ 2 files changed, 20 insertions(+), 13 deletions(-) diff --git a/logging/logfile.go b/logging/logfile.go index ff0f32430..231721750 100644 --- a/logging/logfile.go +++ b/logging/logfile.go @@ -93,23 +93,27 @@ func (l *LogFile) pruneFiles() error { if l.MaxFiles == 0 { return nil } - pattern := l.fileNamePattern() - //get all the files that match the log file pattern - globExpression := filepath.Join(l.logPath, fmt.Sprintf(pattern, "*")) - matches, err := filepath.Glob(globExpression) + + pattern := filepath.Join(l.logPath, fmt.Sprintf(l.fileNamePattern(), "*")) + matches, err := filepath.Glob(pattern) if err != nil { return err } - var stale int - if l.MaxFiles <= -1 { - // Prune everything - stale = len(matches) - } else { - // Prune if there are more files stored than the configured max - stale = len(matches) - l.MaxFiles + + switch { + case l.MaxFiles < 0: + return removeFiles(matches) + case len(matches) < l.MaxFiles: + return nil } - for i := 0; i < stale; i++ { - if err := os.Remove(matches[i]); err != nil { + + last := len(matches) - l.MaxFiles + return removeFiles(matches[:last]) +} + +func removeFiles(files []string) error { + for _, file := range files { + if err := os.Remove(file); err != nil { return err } } diff --git a/logging/logger.go b/logging/logger.go index 0e1292d9c..dfc05785c 100644 --- a/logging/logger.go +++ b/logging/logger.go @@ -92,6 +92,9 @@ func Setup(config Config, out io.Writer) (hclog.InterceptLogger, error) { MaxBytes: config.LogRotateBytes, MaxFiles: config.LogRotateMaxFiles, } + if err := logFile.pruneFiles(); err != nil { + return nil, fmt.Errorf("Failed to prune log files: %w", err) + } if err := logFile.openNew(); err != nil { return nil, fmt.Errorf("Failed to setup logging: %w", err) }