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

Don't timestamp active log file #11070

Merged
merged 4 commits into from
Aug 23, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions .changelog/11070.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
agent: Don't timestamp active log file.
```
17 changes: 12 additions & 5 deletions command/agent/log_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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()
}
Expand Down
2 changes: 2 additions & 0 deletions command/agent/log_file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
6 changes: 3 additions & 3 deletions website/content/docs/configuration/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions website/content/docs/upgrade/upgrade-specific.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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.
schmichael marked this conversation as resolved.
Show resolved Hide resolved

## Nomad 1.0.9 and 1.1.3

#### Namespace in Job Run and Plan APIs
Expand Down Expand Up @@ -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