go - 如何将 zap 记录器与 go-kit 一起使用?

标签 go logging go-zap

我想使用 go-kit logger lib 与 zap我希望它在这个函数中返回实例 我将能够像下面这样使用 zap.logger:(使用 zap 功能)就像

logger.Info

logger.WithOptions

等等

我尝试使用以下方法返回 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 导出自己的日志记录接口(interface)。他们只提供Log方法。它被命名为 zapSugarLogger,它基本上是一个与 zap 的日志函数之一匹配的函数类型(InfowWarnw 等) )。

看起来无法从 zapSugarLogger 实例访问底层 zap 功能。

但是,您可以自己创建一个 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")



}

关于go - 如何将 zap 记录器与 go-kit 一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58325207/

相关文章:

php - 关闭 FPM 日志中的换行符

go - 如何在不使用字段的情况下在 Uber Zap 中记录键/值对

go - Uber-go/zap 和 kafka-go 比赛条件

使用 <(..) 参数执行

go - Google Go 语言中哪些类型是可变的和不可变的?

java - 如何发布 Log4J 记录器

c - 如何用C语言编写日志文件

go - Uber Zap 记录器不在日志语句中打印调用者信息

Golang 和 JWT - 简单注销

google-app-engine - 如果我有 Order 子句,Google App Engine 数据存储区不会返回任何行