Skip to content

Commit

Permalink
do not append timestamp to current log file
Browse files Browse the repository at this point in the history
This patch updates the file_sink to *not* append the timestamp on the
current log file. The timestamp is only appended when the file is
rotated.

This matches the behavior:

- Implemented in Nomad v1.1.4 with hashicorp/nomad#11070
- Requested in hashicorp/nomad#11061
- Referenced in Consul's documentation:
  https://www.consul.io/docs/enterprise/audit-logging

> The following example configures a destination called "My Sink" which
> stores audit events at the file `/tmp/audit.json`.

Nomad's documentation is a little vague but implies the prior behavior
of always appending a timestamp.

While this is a *backward incompatible change* I think it's worth it for
usability and matching Consul's documentation. Nomad's documentation can
be fixed and a notice placed in the Upgrade Guide.
  • Loading branch information
schmichael committed Aug 31, 2021
1 parent a8d9952 commit c3f114d
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 8 deletions.
20 changes: 14 additions & 6 deletions file_sink.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,11 +159,9 @@ func (fs *FileSink) open() error {
}

createTime := time.Now()
// New file name as the format:
// file rotation enabled: filename-timestamp.extension
// file rotation disabled: filename.extension
newfileName := fs.newFileName(createTime)
newfilePath := filepath.Join(fs.Path, newfileName)
// New file name: filename.extension
// Old file name: filename-timestamp.extension
newfilePath := filepath.Join(fs.Path, fs.FileName)

var err error
fs.f, err = os.OpenFile(newfilePath, os.O_APPEND|os.O_WRONLY|os.O_CREATE, mode)
Expand Down Expand Up @@ -198,8 +196,18 @@ func (fs *FileSink) rotate() error {
((elapsed > fs.MaxDuration) && (fs.MaxDuration > 0)) {

fs.f.Close()

// Move current log file to a timestamped file.
rotateTime := time.Now().UnixNano()
rotateFileName := fmt.Sprintf(fs.fileNamePattern(), strconv.FormatInt(rotateTime, 10))
oldPath := filepath.Join(fs.Path, fs.FileName)
newPath := filepath.Join(fs.Path, rotateFileName)
if err := os.Rename(oldPath, newPath); err != nil {
return fmt.Errorf("failed to rotate log file: %v", err)
}

if err := fs.pruneFiles(); err != nil {
return err
return fmt.Errorf("failed to prune log files: %w", err)
}
return fs.open()
}
Expand Down
36 changes: 34 additions & 2 deletions file_sink_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,25 @@ func TestFileSink_TimeRotate(t *testing.T) {
}

want := 2
if got, _ := ioutil.ReadDir(tmpDir); len(got) != want {
got, err := os.ReadDir(tmpDir)
if err != nil {
t.Fatalf("Error listing log directory: %v", err)
}
if len(got) != want {
t.Errorf("Expected %d files, got %v file(s)", want, len(got))
}
found := false
names := []string{}
for _, entry := range got {
names = append(names, entry.Name())
if entry.Name() == fs.FileName {
found = true
break
}
}
if !found {
t.Errorf("Did not find expected file %q -- found: %v", fs.FileName, names)
}
}

func TestFileSink_ByteRotate(t *testing.T) {
Expand Down Expand Up @@ -183,9 +199,25 @@ func TestFileSink_ByteRotate(t *testing.T) {
}

want := 2
if got, _ := ioutil.ReadDir(tmpDir); len(got) != want {
got, err := os.ReadDir(tmpDir)
if err != nil {
t.Fatalf("Error listing log directory: %v", err)
}
if len(got) != want {
t.Errorf("Expected %d files, got %v file(s)", want, len(got))
}
found := false
names := []string{}
for _, entry := range got {
names = append(names, entry.Name())
if entry.Name() == fs.FileName {
found = true
break
}
}
if !found {
t.Errorf("Did not find expected file %q -- found: %v", fs.FileName, names)
}
}

func TestFileSink_open(t *testing.T) {
Expand Down

0 comments on commit c3f114d

Please sign in to comment.