简体   繁体   English

如何在 go-kit 中使用 zap logger?

[英]How to use zap logger with go-kit?

I want to use the go-kit logger lib with zap and I want it in this function to return instance of zap.logger that I will be able to use it like following: (using zap functionality) like我想将go-kit logger lib 与zap一起使用,我希望它在这个 function 中返回 zap.logger 的实例,我将能够像下面这样使用它:(使用 zap 功能)喜欢

logger.Info

or或者

logger.WithOptions

etc ETC

I try with the following to return zap interface but it doesn't work, the methods are not available, any idea what am I missing here?我尝试使用以下方法返回 zap 接口,但它不起作用,方法不可用,知道我在这里缺少什么吗?

func NewZapLogger() zap.Logger  {

   cfg := zap.Config{
      Encoding:         "json",
      Level:            zap.NewAtomicLevelAt(zapcore.DebugLevel),
      OutputPaths:      []string{"stderr"},
      ErrorOutputPaths: []string{"stderr"},
      EncoderConfig: zapcore.EncoderConfig{
         MessageKey: "message",

         LevelKey:    "level",
         EncodeLevel: zapcore.CapitalLevelEncoder,

         TimeKey:    "time",
         EncodeTime: zapcore.ISO8601TimeEncoder,

         CallerKey:    "caller",
         EncodeCaller: zapcore.FullCallerEncoder,
      },
   }
   logger, _ := cfg.Build()

   sugarLogger := logz.NewZapSugarLogger(logger, zap.InfoLevel)

   return sugarLogger.

}
     

Go Kit exports their own logging interface. Go Kit 导出自己的日志接口。 They only provide the Log method.他们只提供Log方法。 It's named zapSugarLogger and it's basically a function type matching one of zap 's logging function ( Infow , Warnw etc).它被命名为zapSugarLogger ,它基本上是一个 function 类型匹配zap的日志记录 function ( InfowWarnw等)之一。

Looks like there is no way to access the underlying zap functionality from the zapSugarLogger instance.看起来没有办法从zapSugarLogger实例访问底层的 zap 功能。

You can however, create an instance of zap yourself and use it as usual.但是,您可以自己创建一个zap实例并照常使用它。

package main

import (
    "go.uber.org/zap"
    "go.uber.org/zap/zapcore"
)

func main() {

    cfg := zap.Config{
        Encoding:         "json",
        Level:            zap.NewAtomicLevelAt(zapcore.DebugLevel),
        OutputPaths:      []string{"stderr"},
        ErrorOutputPaths: []string{"stderr"},
        EncoderConfig: zapcore.EncoderConfig{
            MessageKey: "message",

            LevelKey:    "level",
            EncodeLevel: zapcore.CapitalLevelEncoder,

            TimeKey:    "time",
            EncodeTime: zapcore.ISO8601TimeEncoder,

            CallerKey:    "caller",
            EncodeCaller: zapcore.FullCallerEncoder,
        },
    }
    logger, _ := cfg.Build()

    logger.Info("Hello")



}

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

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