我正在阅读 log source code ,我在这里很困惑:
// SetOutput sets the output destination for the standard logger.
func SetOutput(w io.Writer) {
std.mu.Lock()
defer std.mu.Unlock()
std.out = w
}
// Flags returns the output flags for the standard logger.
func Flags() int {
return std.Flags()
}
// SetFlags sets the output flags for the standard logger.
func SetFlags(flag int) {
std.SetFlags(flag)
}
为什么SetOutPut
使用mu
锁定,而SetFlags
不使用mu
锁定?
最佳答案
SetOutput
正在改变 std
默认 Logger 的内部状态。
log.SetFlags
正在调用 SetFlags
在 std
上,它已经锁定了记录器本身。
关于logging - 为什么不在日志包中使用lock同步,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34904822/