1

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.

}
     
blackgreen
  • 34,072
  • 23
  • 111
  • 129
JME
  • 881
  • 2
  • 11
  • 23

1 Answers1

1

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



}

masnun
  • 11,635
  • 4
  • 39
  • 50
  • Thanks, I saw this thread but I wasnt able to make it work with zap, example will be very appreciated – JME Oct 10 '19 at 14:43
  • If you post your full source code, I would happily modify it and update my answer with it. – masnun Oct 10 '19 at 14:46
  • @JhonD I checked out zap code and how it's used in go-kit. Unfortunately, it's implemented in a different way than logrus. I have updated my answer. – masnun Oct 10 '19 at 14:59