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

huangapple go评论119阅读模式
英文:

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

问题

go-nsq库(https://github.com/bitly/go-nsq/blob/master/writer_test.go#L38)中,我发现了以下代码:

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

为什么作者在将日志丢弃后又延迟将其输出到标准输出(stdout)?

英文:

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

得分: 2

log.SetOutput(ioutil.Discard)语句更改了标准日志记录器的输出目标。defer log.SetOutput(os.Stdout)语句试图在函数结束时将输出目标重置回初始值。然而,它应该将其重置回os.Stderr

src/pkg/log/log.go

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

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)

huangapple
  • 本文由 发表于 2014年3月3日 02:54:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/22131865.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定