defer log.SetOutput(os.Stdout) after log.SetOutput(ioutil.Discard)

1.5k Views Asked by At

In go-nsq library (https://github.com/bitly/go-nsq/blob/master/writer_test.go#L38), I found the following code:

log.SetOutput(ioutil.Discard)
defer log.SetOutput(os.Stdout)

Why does the author defer logging to stdout after discard the log?

1

There are 1 best solutions below

2
On BEST ANSWER

The log.SetOutput(ioutil.Discard) statement changes the standard logger output destination. The defer log.SetOutput(os.Stdout) statement attempts to reset the output destination back to its initial value when the function ends. However, it should have reset it back to os.Stderr.

src/pkg/log/log.go

var std = New(os.Stderr, "", LstdFlags)