Fix Windows logging to files (#11960)
This commit is contained in:
parent
b13fb553ac
commit
c9735476a7
|
@ -0,0 +1,3 @@
|
||||||
|
```release-note:bug
|
||||||
|
windows: Fixes a bug with empty log files when Consul is run as a Windows Service
|
||||||
|
```
|
|
@ -45,6 +45,17 @@ const defaultRotateDuration = 24 * time.Hour
|
||||||
|
|
||||||
type LogSetupErrorFn func(string)
|
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) {
|
||||||
|
_, _ = w.w.Write(p)
|
||||||
|
// We purposely return n == len(p) as if write was successful
|
||||||
|
return len(p), nil
|
||||||
|
}
|
||||||
|
|
||||||
// Setup logging from Config, and return an hclog Logger.
|
// Setup logging from Config, and return an hclog Logger.
|
||||||
//
|
//
|
||||||
// Logs may be written to out, and optionally to syslog, and a file.
|
// Logs may be written to out, and optionally to syslog, and a file.
|
||||||
|
@ -55,7 +66,10 @@ func Setup(config Config, out io.Writer) (hclog.InterceptLogger, error) {
|
||||||
allowedLogLevels)
|
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 {
|
if config.EnableSyslog {
|
||||||
retries := 12
|
retries := 12
|
||||||
|
|
Loading…
Reference in New Issue