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

Fix Windows logging to files #11960

Merged
merged 4 commits into from
Jan 6, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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/11960.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
windows: Fixes a bug with empty log files when Consul is run as a Windows Service
```
23 changes: 18 additions & 5 deletions logging/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,16 @@ type Config struct {
// SyslogFacility is the destination for syslog forwarding.
SyslogFacility string

//LogFilePath is the path to write the logs to the user specified file.
// LogFilePath is the path to write the logs to the user specified file.
LogFilePath string

//LogRotateDuration is the user specified time to rotate logs
// LogRotateDuration is the user specified time to rotate logs
LogRotateDuration time.Duration

//LogRotateBytes is the user specified byte limit to rotate logs
// LogRotateBytes is the user specified byte limit to rotate logs
LogRotateBytes int

//LogRotateMaxFiles is the maximum number of past archived log files to keep
// LogRotateMaxFiles is the maximum number of past archived log files to keep
LogRotateMaxFiles int
}

Expand All @@ -45,6 +45,16 @@ const defaultRotateDuration = 24 * time.Hour

type LogSetupErrorFn func(string)

// noErrorWriter is a wrapper to suppress errors when writing to w.
type noErrorWriter struct {
w io.Writer
}

func (w noErrorWriter) Write(p []byte) (n int, err error) {
n, _ = w.w.Write(p)
return n, nil
}

// Setup logging from Config, and return an hclog Logger.
//
// Logs may be written to out, and optionally to syslog, and a file.
Expand All @@ -55,7 +65,10 @@ func Setup(config Config, out io.Writer) (hclog.InterceptLogger, error) {
allowedLogLevels)
}

writers := []io.Writer{out}
// If out is os.Stdout and Consul is being run as a Windows Service, writes will
// fail silently, which may inadvertently prevent writes to other writers.
// noErrorWriter is used as a wrapper to suppress any errors when writing to out.
writers := []io.Writer{noErrorWriter{w: out}}

if config.EnableSyslog {
retries := 12
Expand Down