JME
JME

Reputation: 949

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

logger.Info

or

logger.WithOptions

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?

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.

}
     

Upvotes: 1

Views: 1649

Answers (1)

masnun
masnun

Reputation: 11916

Go Kit exports their own logging interface. They only provide the Log method. It's named zapSugarLogger and it's basically a function type matching one of zap's logging function (Infow, Warnw etc).

Looks like there is no way to access the underlying zap functionality from the zapSugarLogger instance.

You can however, create an instance of zap yourself and use it as usual.

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")



}

Upvotes: 1

Related Questions