简体   繁体   English

Uber Zap 记录器 function 日志中的名称

[英]Uber Zap logger function name in logs

How to get function name printed in logs from Uber Zap logging?如何从 Uber Zap 日志中获取 function 名称打印在日志中? This is the PR request with which they seemed to have added the functionality to output function names in log.这是PR 请求,他们似乎已将功能添加到日志中的 output function 名称中。

I am using golang version 1.15 and go.uber.org/zap v1.16.0我正在使用 golang 版本1.15和 go.uber.org/zap v1.16.0

This is my code:这是我的代码:

 package main import ( "go.uber.org/zap" ) var logger *zap.Logger func main() { logger:= NewLogger() logger.Info("Test msg Main") TestFunc(logger) } func TestFunc(logger *zap.Logger) { logger.Info("Test msg TestFunc") } func NewLogger() *zap.Logger { config:= zap.NewDevelopmentConfig() opts:= []zap.Option{ zap.AddCallerSkip(1), // traverse call depth for more useful log lines zap.AddCaller(), } logger, _ = config.Build(opts...) return logger }

This is the output I get with/without the addition of AddCaller() option这是 output 有/没有添加AddCaller()选项

2021-03-01T15:00:02.927-0800    INFO    runtime/proc.go:204     Test msg Main
2021-03-01T15:00:02.927-0800    INFO    cmd/main.go:12  Test msg TestFunc

I am expecting something like我期待类似的东西

   2021-03-01T15:00:02.927-0800    INFO    runtime/proc.go:204   main  Test msg Main
   2021-03-01T15:00:02.927-0800    INFO    cmd/main.go:12  TestFunc Test msg TestFunc

By default, the provided encoder presets ( NewDevelopmentEncoderConfig used by NewDevelopmentConfig and NewProductionEncoderConfig used by NewProductionConfig ) do not enable function name logging.默认情况下,提供的编码器预设( NewDevelopmentConfig使用的NewDevelopmentEncoderConfigNewProductionConfig使用的NewProductionEncoderConfig )不启用 function 名称记录。

To enable function name, you need to enable caller (true by default) and set a non-empty value for config.EncoderConfig.FunctionKey .要启用 function 名称,您需要启用 caller(默认为 true)config.EncoderConfig.FunctionKey设置一个非空值。

Source: EncoderConfig来源: 编码器配置

type EncoderConfig struct {
    // Set the keys used for each log entry. If any key is empty, that portion
    // of the entry is omitted.
    ...
    CallerKey     string `json:"callerKey" yaml:"callerKey"`
    FunctionKey   string `json:"functionKey" yaml:"functionKey"` // this needs to be set
    StacktraceKey string `json:"stacktraceKey" yaml:"stacktraceKey"`
    ...
}

Example Console Logger:示例控制台记录器:

func main() {
    config := zap.NewDevelopmentConfig()
    // if you're using console encoding, the FunctionKey value can be any
    // non-empty string because console encoding does not print the key.
    config.EncoderConfig.FunctionKey = "F"
    logger, _ := config.Build()
    logger.Info("Test Logging")
    // Output: 2021-03-03T11:41:47.728+0800    INFO    example/main.go:11     main.main       Test Logging
}

Example JSON Logger:示例 JSON 记录器:

func main() {
    config := zap.NewProductionConfig()
    // the FunctionKey value matters because it will become the JSON field
    config.EncoderConfig.FunctionKey = "func"
    logger, _ := config.Build()
    log(logger)
    // Output: {"level":"info","ts":1614743088.538128,"caller":"example/main.go:15","func":"main.log","msg":"Test Logging"}
}

func log(logger *zap.Logger) {
    logger.Info("Test Logging")
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM