Search code examples
logginggonsqgo-nsq

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


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?


Solution

  • 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)