Skip to content

Commit

Permalink
Don't timestamp active log file (#11070)
Browse files Browse the repository at this point in the history
* don't timestamp active log file

* website: update log_file default value

* changelog: add entry for #11070

* website: add upgrade instructions for log_file in v1.14 and v1.2.0
  • Loading branch information
lgfa29 authored and Mahmood Ali committed Aug 26, 2021
1 parent 250268a commit 09fd5cf
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 8 deletions.
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
22 changes: 22 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,27 @@ 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

The Job Run and Plan APIs now respect the `?namespace=...` query parameter over
the namespace specified in the job itself. This matches the precedence of
region and [fixes a bug where the `-namespace` flag was not respected for the
`nomad run` and `nomad apply` commands.][gh-10875]

For users of [`api.Client`][go-client] who want their job namespace respected,
you must ensure the `Config.Namespace` field is unset.

## Nomad 1.1.3

#### Docker Driver
Expand Down Expand Up @@ -1159,3 +1180,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

0 comments on commit 09fd5cf

Please sign in to comment.