From bfa612ccee38143a3a288d7c7bc101846ca63e58 Mon Sep 17 00:00:00 2001 From: hc-github-team-consul-core Date: Mon, 4 Sep 2023 05:13:31 -0500 Subject: [PATCH] Backport of NET-3181 - Allow log file naming like Nomad into release/1.15.x (#18629) * backport of commit a5ad3664038a3dd225b5c14b3adc181fb4dd61a6 * backport of commit 24e7b9b9602b83dd67096a61b9305a37c6d77801 * NET-3181 - Allow log file naming like Nomad (#18617) * fixes file name for consul * added log file * added tests for rename method --------- Co-authored-by: absolutelightning Co-authored-by: Ashesh Vidyut <134911583+absolutelightning@users.noreply.github.com> --- .changelog/18617.txt | 4 ++++ logging/logfile.go | 20 +++++++++++++++++--- logging/logfile_test.go | 16 ++++++++++++++++ 3 files changed, 37 insertions(+), 3 deletions(-) create mode 100644 .changelog/18617.txt diff --git a/.changelog/18617.txt b/.changelog/18617.txt new file mode 100644 index 0000000000..1f840d836d --- /dev/null +++ b/.changelog/18617.txt @@ -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. +``` \ No newline at end of file diff --git a/logging/logfile.go b/logging/logfile.go index fbd1d2b707..12cf86d14b 100644 --- a/logging/logfile.go +++ b/logging/logfile.go @@ -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 @@ -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 } diff --git a/logging/logfile_test.go b/logging/logfile_test.go index 09313a67cb..64b051fda6 100644 --- a/logging/logfile_test.go +++ b/logging/logfile_test.go @@ -48,6 +48,22 @@ func TestLogFile_openNew(t *testing.T) { require.Contains(t, string(content), msg) } +func TestLogFile_renameCurrentFile(t *testing.T) { + logFile := LogFile{ + fileName: "consul.log", + logPath: testutil.TempDir(t, ""), + duration: defaultRotateDuration, + } + err := logFile.openNew() + require.NoError(t, err) + + err = logFile.renameCurrentFile() + require.NoError(t, err) + + _, err = os.ReadFile(logFile.FileInfo.Name()) + require.Contains(t, err.Error(), "no such file or directory") +} + func TestLogFile_Rotation_MaxBytes(t *testing.T) { tempDir := testutil.TempDir(t, "LogWriterBytes") logFile := LogFile{