diff --git a/.changelog/11070.txt b/.changelog/11070.txt new file mode 100644 index 000000000..98f25b603 --- /dev/null +++ b/.changelog/11070.txt @@ -0,0 +1,3 @@ +```release-note:bug +agent: Don't timestamp active log file. +``` diff --git a/command/agent/log_file.go b/command/agent/log_file.go index bad3c6c17..da660c4ea 100644 --- a/command/agent/log_file.go +++ b/command/agent/log_file.go @@ -62,11 +62,8 @@ func (l *logFile) fileNamePattern() string { } func (l *logFile) openNew() error { - fileNamePattern := l.fileNamePattern() - // New file name has the format : filename-timestamp.extension createTime := now() - newfileName := fmt.Sprintf(fileNamePattern, strconv.FormatInt(createTime.UnixNano(), 10)) - newfilePath := filepath.Join(l.logPath, newfileName) + newfilePath := filepath.Join(l.logPath, l.fileName) // Try creating a file. We truncate the file because we are the only authority to write the logs filePointer, err := os.OpenFile(newfilePath, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0640) if err != nil { @@ -86,8 +83,18 @@ func (l *logFile) rotate() error { // 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() + + // Move current log file to a timestamped file. + rotateTime := now() + rotatefileName := fmt.Sprintf(l.fileNamePattern(), strconv.FormatInt(rotateTime.UnixNano(), 10)) + oldPath := l.FileInfo.Name() + newPath := filepath.Join(l.logPath, rotatefileName) + if err := os.Rename(oldPath, newPath); err != nil { + return fmt.Errorf("failed to rotate log files: %v", err) + } + if err := l.pruneFiles(); err != nil { - return err + return fmt.Errorf("failed to prune log files: %v", err) } return l.openNew() } diff --git a/command/agent/log_file_test.go b/command/agent/log_file_test.go index 12777784d..402560ac0 100644 --- a/command/agent/log_file_test.go +++ b/command/agent/log_file_test.go @@ -55,6 +55,8 @@ func TestLogFile_openNew(t *testing.T) { _, err = ioutil.ReadFile(logFile.FileInfo.Name()) require.NoError(err) + + require.Equal(logFile.FileInfo.Name(), filepath.Join(tempDir, testFileName)) } func TestLogFile_byteRotation(t *testing.T) { diff --git a/website/content/docs/configuration/index.mdx b/website/content/docs/configuration/index.mdx index 204d249e1..1d3d2bf37 100644 --- a/website/content/docs/configuration/index.mdx +++ b/website/content/docs/configuration/index.mdx @@ -241,9 +241,9 @@ testing. - `log_json` `(bool: false)` - Output logs in a JSON format. - `log_file` `(string: "")` - Specifies the path for logging. If the path - does not includes a filename, the filename defaults to "nomad-{timestamp}.log". - This setting can be combined with `log_rotate_bytes` and `log_rotate_duration` - for a fine-grained log rotation control. + does not includes a filename, the filename defaults to `nomad.log`. This + setting can be combined with `log_rotate_bytes` and `log_rotate_duration` for + a fine-grained log rotation control. - `log_rotate_bytes` `(int: 0)` - Specifies the number of bytes that should be written to a log before it needs to be rotated. Unless specified, there is no diff --git a/website/content/docs/upgrade/upgrade-specific.mdx b/website/content/docs/upgrade/upgrade-specific.mdx index b5c02c8d1..c0dcd04c8 100644 --- a/website/content/docs/upgrade/upgrade-specific.mdx +++ b/website/content/docs/upgrade/upgrade-specific.mdx @@ -13,6 +13,15 @@ upgrade. However, specific versions of Nomad may have more details provided for their upgrades as a result of new features or changed behavior. This page is used to document those details separately from the standard upgrade flow. +## Nomad 1.1.4 and 1.2.0 + +#### Log file names + +The [`log_file`] configuration option was not being fully respected, as the +generated filename would include a timestamp. After upgrade, the active log +file will always be the value defined in `log_file`, with timestamped files +being created during log rotation. + ## Nomad 1.0.9 and 1.1.3 #### Namespace in Job Run and Plan APIs @@ -1173,3 +1182,4 @@ deleted and then Nomad 0.3.0 can be launched. [allow_caps_java]: /docs/drivers/java#allow_caps [cap_add_exec]: /docs/drivers/exec#cap_add [cap_drop_exec]: /docs/drivers/exec#cap_drop +[`log_file`]: /docs/configuration#log_file