英文:
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.Field
s.
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 Field
s, but you included too many strings and no Field
s. The string
s can't be Field
s, 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
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论