Skip to content

Commit

Permalink
Include date separator in the filename prefix of dateRotator to mak…
Browse files Browse the repository at this point in the history
…e sure nothing gets purged accidentally (#26176) (#26178)

## What does this PR do?

This PR changes the log file prefix when using date rotation in Beats. Previously the `-` was not included, so in the list of rotated files every file that started with the configured file prefix were included.

## Why is it important?

If the binary is under the same path as the value configured in `logging.files.path` and `logging.files.name`, when the number of rotated log files gets bigger than the one configured in `loggin.files.keepfiles`, the binary is purged on rotation.

The workaround is to put the log files in a separate folder.

(cherry picked from commit 447bac9)

Co-authored-by: Noémi Ványi <kvch@users.noreply.github.com>
  • Loading branch information
mergify[bot] and kvch authored Jun 8, 2021
1 parent b2198e2 commit cf89d91
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha2...master[Check the HEAD d
- Fix out of date FreeBSD vagrantbox. {pull}25652[25652]
- Fix handling of `file_selectors` in aws-s3 input. {pull}25792[25792]
- Fix ILM alias creation when write alias exists and initial index does not exist {pull}26143[26143]
- Include date separator in the filename prefix of `dateRotator` to make sure nothing gets purged accidentally {pull}26176[26176]

*Auditbeat*

Expand Down
8 changes: 4 additions & 4 deletions libbeat/common/file/rotator.go
Original file line number Diff line number Diff line change
Expand Up @@ -435,11 +435,11 @@ func newRotater(log Logger, s SuffixType, filename string, maxBackups uint, inte
func newDateRotater(log Logger, filename string) rotater {
d := &dateRotator{
log: log,
filenamePrefix: filename,
filenamePrefix: filename + "-",
format: "20060102150405",
}

d.currentFilename = d.filenamePrefix + "-" + time.Now().Format(d.format)
d.currentFilename = d.filenamePrefix + time.Now().Format(d.format)
files, err := filepath.Glob(d.filenamePrefix + "*")
if err != nil {
return d
Expand Down Expand Up @@ -467,7 +467,7 @@ func (d *dateRotator) Rotate(reason rotateReason, rotateTime time.Time) error {
d.log.Debugw("Rotating file", "filename", d.currentFilename, "reason", reason)
}

d.currentFilename = d.filenamePrefix + "-" + rotateTime.Format(d.format)
d.currentFilename = d.filenamePrefix + rotateTime.Format(d.format)
return nil
}

Expand All @@ -493,7 +493,7 @@ func (d *dateRotator) SortModTimeLogs(strings []string) {
}

func (d *dateRotator) OrderLog(filename string) time.Time {
ts, err := time.Parse(d.format, filepath.Base(filename))
ts, err := time.Parse(d.filenamePrefix+d.format, filepath.Base(filename))
if err != nil {
return time.Time{}
}
Expand Down

0 comments on commit cf89d91

Please sign in to comment.