Skip to content

Commit

Permalink
Use a wrapper instead of UiWriter
Browse files Browse the repository at this point in the history
  • Loading branch information
kisunji committed Jan 6, 2022
1 parent 15b396d commit 2917d0e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
5 changes: 1 addition & 4 deletions command/agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,7 @@ func (c *cmd) run(args []string) int {

// FIXME: logs should always go to stderr, but previously they were sent to
// stdout, so continue to use Stdout for now, and fix this in a future release.
// UiWriter is being used here so that writes to Stdout do not error (e.g.
// Windows Services are non-interactive and do not have Stdout) and inadvertently
// stop writes to other logging outputs like syslogs or log files.
logGate := &logging.GatedWriter{Writer: &mcli.UiWriter{Ui: c.ui}}
logGate := &logging.GatedWriter{Writer: c.ui.Stdout()}
loader := func(source config.Source) (config.LoadResult, error) {
c.configLoadOpts.DefaultConfig = source
return config.Load(c.configLoadOpts)
Expand Down
15 changes: 14 additions & 1 deletion logging/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 2917d0e

Please sign in to comment.