package log import ( "fmt" "os" "github.com/go-kratos/kratos/v2/log" "go.uber.org/zap" "go.uber.org/zap/zapcore" ) var _ log.Logger = (*ZapLogger)(nil) type ZapLogger struct { log *zap.Logger Sync func() error } // NewZapLogger return ZapLogger func NewZapLogger(encoder zapcore.EncoderConfig, level zap.AtomicLevel, opts ...zap.Option) *ZapLogger { core := zapcore.NewCore( zapcore.NewConsoleEncoder(encoder), zapcore.NewMultiWriteSyncer( zapcore.AddSync(os.Stdout), ), level) zapLogger := zap.New(core, opts...) return &ZapLogger{log: zapLogger, Sync: zapLogger.Sync} } // Log Implementation of logger interface func (l *ZapLogger) Log(level log.Level, keyvals ...interface{}) error { if len(keyvals) == 0 || len(keyvals)%2 != 0 { l.log.Warn(fmt.Sprint("Keyvalues must appear in pairs: ", keyvals)) return nil } // Zap.Field is used when keyvals pairs appear var data []zap.Field for i := 0; i < len(keyvals); i += 2 { data = append(data, zap.Any(fmt.Sprint(keyvals[i]), fmt.Sprint(keyvals[i+1]))) } switch level { case log.LevelDebug: l.log.Debug("", data...) case log.LevelInfo: l.log.Info("", data...) case log.LevelWarn: l.log.Warn("", data...) case log.LevelError: l.log.Error("", data...) } return nil }