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.
This commit is contained in:
Danielle Lancashire 2019-10-10 13:33:08 +02:00
parent 9eaac48f25
commit b67215d4f8
No known key found for this signature in database
GPG Key ID: 8D65584EF3DDF91B
2 changed files with 7 additions and 1 deletions

View File

@ -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++ {

View File

@ -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")
}
}