Andrey Asked:2020-05-27 21:26:20 +0800 CST2020-05-27 21:26:20 +0800 CST 2020-05-27 21:26:20 +0800 CST Golang 中的 zap.Logger 线程安全吗? 772 Golang 中的 zap.Logger 线程安全吗? golang 1 个回答 Voted Best Answer AivanF. 2020-05-27T22:26:08+08:002020-05-27T22:26:08+08:00 兴趣问。我在 GitHub 上发现了一个关于这个主题的 issue,有这样一个代码示例: package main import "github.com/uber-go/zap" var logger zap.Logger func main() { logger = zap.NewJSON() for i := 0; i < 800; i++ { go printer(i) } } func printer(i int) { logger.Info("Counting", zap.Int("i", i), ) } 这给出了(给定的)这种输出: {"msg":"Counting","level":"info","ts":1469196244924860064,"fields":{"i":8}} {"msg":"Counting","level":"info","ts":1469196244924867134,"fields":{"i":42}} {"msg":"Counting","level":"info","ts":1469196244924866589,"fields":{"i":20}} {"msg":"Counti{"msg":"Counting","level":"info","ts":1469196244924879429,"fields":{"i":21}} ng","level":"info","ts":1469196244924870959,"fields":{"i":9}} {"msg":"Counting","level":"info","ts":1469196244924879050,"fields":{"i":36}} {"msg":"Counting","level":"info","ts":1469196244924873937,"fields":{"i":43}} {"msg":"Counting","level":"info","ts":1469196244924904543,"fields":{"i":22}} {"msg":"Counting","level":"info","ts":1469196244924910632,"fields":{"i":10}} {"msg":"Counting","level":"info","ts":1469196244924916792,"fields":{"i":23}} ... 您可以在第 4 行看到叠加层。但显然问题几乎同时解决了。在所有运行中,此示例的更新版本会产生偶数列的输出: package main import "go.uber.org/zap" var logger *zap.Logger func main() { logger, _ = zap.NewProduction() for i := 0; i < 800; i++ { go printer(i) } } func printer(i int) { logger.Info("Counting", zap.Int("i", i), ) }
兴趣问。我在 GitHub 上发现了一个关于这个主题的 issue,有这样一个代码示例:
这给出了(给定的)这种输出:
您可以在第 4 行看到叠加层。但显然问题几乎同时解决了。在所有运行中,此示例的更新版本会产生偶数列的输出: