NET-3181 - Allow log file naming like Nomad (#18617)

* fixes file name for consul

* added log file

* added tests for rename method
pull/18634/head
Ashesh Vidyut 1 year ago committed by GitHub
parent f2ce472ae1
commit d45c3c2755
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -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.
```

@ -60,10 +60,8 @@ func (l *LogFile) fileNamePattern() string {
} }
func (l *LogFile) openNew() error { func (l *LogFile) openNew() error {
fileNamePattern := l.fileNamePattern()
createTime := now() createTime := now()
newfileName := fmt.Sprintf(fileNamePattern, strconv.FormatInt(createTime.UnixNano(), 10)) newfileName := l.fileName
newfilePath := filepath.Join(l.logPath, newfileName) newfilePath := filepath.Join(l.logPath, newfileName)
// Try creating a file. We truncate the file because we are the only authority to write the logs // Try creating a file. We truncate the file because we are the only authority to write the logs
@ -79,12 +77,28 @@ func (l *LogFile) openNew() error {
return nil 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 { func (l *LogFile) rotate() error {
// Get the time from the last point of contact // Get the time from the last point of contact
timeElapsed := time.Since(l.LastCreated) timeElapsed := time.Since(l.LastCreated)
// Rotate if we hit the byte file limit or the time limit // Rotate if we hit the byte file limit or the time limit
if (l.BytesWritten >= int64(l.MaxBytes) && (l.MaxBytes > 0)) || timeElapsed >= l.duration { if (l.BytesWritten >= int64(l.MaxBytes) && (l.MaxBytes > 0)) || timeElapsed >= l.duration {
l.FileInfo.Close() l.FileInfo.Close()
if err := l.renameCurrentFile(); err != nil {
return err
}
if err := l.pruneFiles(); err != nil { if err := l.pruneFiles(); err != nil {
return err return err
} }

@ -51,6 +51,22 @@ func TestLogFile_openNew(t *testing.T) {
require.Contains(t, string(content), msg) 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) { func TestLogFile_Rotation_MaxBytes(t *testing.T) {
tempDir := testutil.TempDir(t, "LogWriterBytes") tempDir := testutil.TempDir(t, "LogWriterBytes")
logFile := LogFile{ logFile := LogFile{

Loading…
Cancel
Save