logging - 为什么不在日志包中使用lock同步

标签 logging go

我正在阅读 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 正在调用 SetFlagsstd 上,它已经锁定了记录器本身。

关于logging - 为什么不在日志包中使用lock同步,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34904822/

相关文章:

javascript - console.log 是如何工作的?

go - 为什么goroutine中的未缓冲 channel 获得了此顺序

go - 接口(interface)定义中方法签名中的接口(interface)

cookies - 用户 cookie 验证随机失败

go - 如何使用 reflect.DeepEqual() 将指针的值与其类型的零值进行比较?

go - 如何使用 go-redis/redis 包在 Go 中创建 Redis 事务?

c# - 寻找 .NET 的自动日志记录工具/库

c# - 如何优雅地记录每条消息的上下文信息

java - 如何在变量值内的sql语句中转义单引号?

asp.net - 如何从 Azure Web 角色/Web Worker 记录硬件信息?