Skip to content

Commit

Permalink
Merge a5ad366 into backport/NET-3181-consul-GH-Issue-15709-Allow-log-…
Browse files Browse the repository at this point in the history
…file-naming-like-Nomad/slowly-intense-seal
  • Loading branch information
hc-github-team-consul-core authored Aug 31, 2023
2 parents 64a88dc + a5ad366 commit 604bd6e
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions logging/logfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,8 @@ func (l *LogFile) fileNamePattern() string {
}

func (l *LogFile) openNew() error {
fileNamePattern := l.fileNamePattern()

createTime := now()
newfileName := fmt.Sprintf(fileNamePattern, strconv.FormatInt(createTime.UnixNano(), 10))
newfileName := l.fileName
newfilePath := filepath.Join(l.logPath, newfileName)

// Try creating a file. We truncate the file because we are the only authority to write the logs
Expand All @@ -79,12 +77,28 @@ func (l *LogFile) openNew() error {
return nil
}

func (l *LogFile) renameCurrentFile() error {
fileNamePattern := l.fileNamePattern()

createTime := now()
// Current file is consul.log always
currentFilePath := filepath.Join(l.logPath, l.fileName)

oldFileName := fmt.Sprintf(fileNamePattern, strconv.FormatInt(createTime.UnixNano(), 10))
oldFilePath := filepath.Join(l.logPath, oldFileName)

return os.Rename(currentFilePath, oldFilePath)
}

func (l *LogFile) rotate() error {
// Get the time from the last point of contact
timeElapsed := time.Since(l.LastCreated)
// Rotate if we hit the byte file limit or the time limit
if (l.BytesWritten >= int64(l.MaxBytes) && (l.MaxBytes > 0)) || timeElapsed >= l.duration {
l.FileInfo.Close()
if err := l.renameCurrentFile(); err != nil {
return err
}
if err := l.pruneFiles(); err != nil {
return err
}
Expand Down

0 comments on commit 604bd6e

Please sign in to comment.