Zap全局记录器

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

Zap global logger

问题

你好,我想使用zap全局日志记录器。

目前我正在使用sugar_logger,像这样:

log_sugar.Infow("Start API",
	"protocol", "http",
	"host", config.Host,
)

响应是:

{"level":"info","ts":1638893668.7487385,"caller":"root/main.go:97","msg":"Start API.","host":"0.0.0.0:8080","protocol":"http","host":"0.0.0.0:8080"}

但是我想像这样使用:
zap.L().Info("replaced zap's global loggers")

当我这样使用时:

zap.L().Info("Start API",
	"protocol", "http",
	"host", config.Host,
)

我得到一个错误"too many arguments",我该如何使用全局日志记录器,我做错了什么?

谢谢。

英文:

Hello I want to use zap global logger

Right now I am using sugar_logger like this

		log_sugar.Infow("Start API",
			"protocol", "http",
			"host", config.Host,
		)

And response is {"level":"info","ts":1638893668.7487385,"caller":"root/main.go:97","msg":"Start API.","host":"0.0.0.0:8080","protocol":"http","host":"0.0.0.0:8080"}

but I want to use like this zap.L().Info("replaced zap's global loggers")

when I use like this

zap.L().Info("Start API",
			"protocol", "http",
			"host", config.Host,
		)

I get an error "too many arguments" how can I use global logger, what am I doing wrong

Thank you

答案1

得分: 0

https://pkg.go.dev/go.uber.org/zap#Logger 接受一个格式化的 string 和一个 zap.Field 列表。

zap.L().Info("Start API",
            zap.String("protocol","http"),
            zap.String("host",config.Host),
)

因为 zap.Info 接受一个字符串和一个可变参数的 Field 列表,但你包含了太多的字符串而没有 Field。这些字符串不能作为 Field,所以它们被视为额外的字符串参数,不符合函数签名。

英文:

https://pkg.go.dev/go.uber.org/zap#Logger takes a format string and a list of zap.Fields.

zap.L().Info("Start API",
            zap.String("protocol","http"),
            zap.String("host",config.Host),
)

"too many arguments" because zap.Info takes a string and a variadic list of Fields, but you included too many strings and no Fields. The strings can't be Fields, so they ended up being extra string arguments and that doesn't fit the function signature.

答案2

得分: 0

请使用zap.S()代替,它返回全局SugaredLogger的指针。

英文:

Use zap.S() instead, which returns a pointer to the global SugaredLogger.

huangapple
  • 本文由 发表于 2021年12月8日 00:17:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/70263522.html
匿名

发表评论

匿名网友

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

确定