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

Net 3181 consul gh issue 15709 allow log file naming like nomad - fix bug #18631

Merged
3 changes: 3 additions & 0 deletions .changelog/18631.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
absolutelightning marked this conversation as resolved.
Show resolved Hide resolved
agent: Fixed an issue that could cause previous log lines to be overwritten
```
13 changes: 9 additions & 4 deletions logging/logfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,19 +60,24 @@
}

func (l *LogFile) openNew() error {
createTime := now()
newfileName := l.fileName
newfilePath := filepath.Join(l.logPath, newfileName)

// 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)
// Try creating or opening the active log file. Since the active log file
// always has the same name, append log entries to prevent overwriting
// previous log data.
filePointer, err := os.OpenFile(newfilePath, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0640)
if err != nil {
return err
}

l.FileInfo = filePointer
stat, err := filePointer.Stat()
if err != nil {
return err
}
// New file, new bytes tracker, new creation time :)
l.LastCreated = createTime
l.LastCreated = l.createTime(stat)

Check failure on line 80 in logging/logfile.go

View workflow job for this annotation

GitHub Actions / build-amd64

l.createTime undefined (type *LogFile has no field or method createTime)
l.BytesWritten = 0
return nil
}
Expand Down
16 changes: 16 additions & 0 deletions logging/logfile_bsd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//go:build darwin || freebsd || netbsd || openbsd
// +build darwin freebsd netbsd openbsd

package logging

import (
"os"
"syscall"
"time"
)

func (l *LogFile) createTime(stat os.FileInfo) time.Time {
stat_t := stat.Sys().(*syscall.Stat_t)
createTime := stat_t.Ctimespec
return time.Unix(int64(createTime.Sec), int64(createTime.Nsec))
}
17 changes: 17 additions & 0 deletions logging/logfile_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//go:build dragonfly || linux || solaris
// +build dragonfly linux solaris

package logging

import (
"os"
"syscall"
"time"
)

func (l *LogFile) createTime(stat os.FileInfo) time.Time {
stat_t := stat.Sys().(*syscall.Stat_t)
createTime := stat_t.Ctim
// Sec and Nsec are int32 in 32-bit architectures.
return time.Unix(int64(createTime.Sec), int64(createTime.Nsec)) //nolint:unconvert
}
14 changes: 14 additions & 0 deletions logging/logfile_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package logging

import (
"os"
"time"
)

func (l *LogFile) createTime(stat os.FileInfo) time.Time {
// Use `ModTime` as an approximation if the exact create time is not
// available.
// On Windows, the file create time is not updated after the active log
// rotates, so use `ModTime` as an approximation as well.
return stat.ModTime()
}
Loading