Skip to content

Commit

Permalink
[file] rotator purge fix (#156)
Browse files Browse the repository at this point in the history
only include files for consideration of purging that include the
extension
  • Loading branch information
leehinman authored Oct 25, 2023
1 parent b0b2509 commit 734f0e1
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
2 changes: 1 addition & 1 deletion file/rotator.go
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,7 @@ func (d *dateRotator) Rotate(reason rotateReason, rotateTime time.Time) error {
}

func (d *dateRotator) RotatedFiles() []string {
files, err := filepath.Glob(d.filenamePrefix + "*")
files, err := filepath.Glob(d.filenamePrefix + "*" + d.extension)
if err != nil {
if d.log != nil {
d.log.Debugw("failed to list existing logs: %+v", err)
Expand Down
11 changes: 6 additions & 5 deletions file/rotator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ func TestDailyRotation(t *testing.T) {
logname + "-" + twoDaysAgo + "-1.ndjson",
logname + "-" + twoDaysAgo + "-2.ndjson",
logname + "-" + twoDaysAgo + "-3.ndjson",
logname + "-diagnostic-" + twoDaysAgo + ".zip",
}

for _, f := range files {
Expand All @@ -153,26 +154,26 @@ func TestDailyRotation(t *testing.T) {

Rotate(t, r)

AssertDirContents(t, dir, logname+"-"+yesterday+"-12.ndjson", logname+"-"+yesterday+"-13.ndjson")
AssertDirContents(t, dir, logname+"-"+yesterday+"-12.ndjson", logname+"-"+yesterday+"-13.ndjson", logname+"-diagnostic-"+twoDaysAgo+".zip")

WriteMsg(t, r)

today := time.Now().Format(file.DateFormat)
AssertDirContents(t, dir, logname+"-"+yesterday+"-12.ndjson", logname+"-"+yesterday+"-13.ndjson", logname+"-"+today+".ndjson")
AssertDirContents(t, dir, logname+"-"+yesterday+"-12.ndjson", logname+"-"+yesterday+"-13.ndjson", logname+"-"+today+".ndjson", logname+"-diagnostic-"+twoDaysAgo+".zip")

Rotate(t, r)

AssertDirContents(t, dir, logname+"-"+yesterday+"-13.ndjson", logname+"-"+today+".ndjson")
AssertDirContents(t, dir, logname+"-"+yesterday+"-13.ndjson", logname+"-"+today+".ndjson", logname+"-diagnostic-"+twoDaysAgo+".zip")

WriteMsg(t, r)

AssertDirContents(t, dir, logname+"-"+yesterday+"-13.ndjson", logname+"-"+today+".ndjson", logname+"-"+today+"-1.ndjson")
AssertDirContents(t, dir, logname+"-"+yesterday+"-13.ndjson", logname+"-"+today+".ndjson", logname+"-"+today+"-1.ndjson", logname+"-diagnostic-"+twoDaysAgo+".zip")

for i := 0; i < (int(maxSizeBytes)/len(logMessage))+1; i++ {
WriteMsg(t, r)
}

AssertDirContents(t, dir, logname+"-"+today+"-1.ndjson", logname+"-"+today+"-2.ndjson", logname+"-"+today+"-3.ndjson")
AssertDirContents(t, dir, logname+"-"+today+"-1.ndjson", logname+"-"+today+"-2.ndjson", logname+"-"+today+"-3.ndjson", logname+"-diagnostic-"+twoDaysAgo+".zip")
}

// Tests the FileConfig.RotateOnStartup parameter
Expand Down

0 comments on commit 734f0e1

Please sign in to comment.