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:
parent
9eaac48f25
commit
b67215d4f8
|
@ -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++ {
|
||||
|
|
|
@ -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")
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue