From b67215d4f8651a016ae98cb2fae069fc8705f271 Mon Sep 17 00:00:00 2001 From: Danielle Lancashire Date: Thu, 10 Oct 2019 13:33:08 +0200 Subject: [PATCH] logging: Sort files when pruning old logs Currently this logging implementation is dependent on the order of files as returned by filepath.Glob, which although internal methods are documented to be lexographical, does not publicly document this. Here we defensively resort. --- command/agent/log_file.go | 6 ++++++ command/agent/log_file_test.go | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/command/agent/log_file.go b/command/agent/log_file.go index 4e0833df9..39ffd43d1 100644 --- a/command/agent/log_file.go +++ b/command/agent/log_file.go @@ -4,6 +4,7 @@ import ( "fmt" "os" "path/filepath" + "sort" "strconv" "strings" "sync" @@ -104,6 +105,11 @@ func (l *logFile) pruneFiles() error { if err != nil { return err } + + // Stort the strings as filepath.Glob does not publicly guarantee that files + // are sorted, so here we add an extra defensive sort. + sort.Strings(matches) + // Prune if there are more files stored than the configured max stale := len(matches) - l.MaxFiles for i := 0; i < stale; i++ { diff --git a/command/agent/log_file_test.go b/command/agent/log_file_test.go index dde394276..12777784d 100644 --- a/command/agent/log_file_test.go +++ b/command/agent/log_file_test.go @@ -140,7 +140,7 @@ func TestLogFile_deleteArchives(t *testing.T) { } contents := string(bytes) - require.NotEqual("[INFO] Hellow World", contents, "oldest log should have been deleted") + require.NotEqual("[INFO] Hello World", contents, "oldest log should have been deleted") } }