0

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?

Mingyu
  • 26,145
  • 13
  • 50
  • 58

1 Answers1

1

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)
peterSO
  • 134,261
  • 26
  • 231
  • 232
  • Thanks, @peterSO! Why would the author want to dump the log to `/dev/null` and set it back later? Isn't logging the error in Unit Test a favorable thing? – Mingyu Mar 02 '14 at 19:24
  • @Mingyu: The tests only care that there is no error, that is, err == nil. The text of logged informational and error messages is considered to be unimportant or clutter. Run the tests without discarding the logged messages. Does it look sensible to discard them? – peterSO Mar 02 '14 at 19:44