Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backport of Net 3181 consul gh issue 15709 allow log file naming like nomad - fix bug into release/1.15.x #18641

Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .changelog/18617.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
```release-note:improvement
log: Currently consul logs files like this consul-{timestamp}.log. This change makes sure that there is always
consul.log file with the latest logs in it.
```
20 changes: 17 additions & 3 deletions logging/logfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,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 @@ -76,12 +74,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
Loading