2021-03-30 22:46:33 +00:00
|
|
|
package logger
|
|
|
|
|
|
|
|
import (
|
|
|
|
"go.uber.org/zap"
|
|
|
|
"go.uber.org/zap/zapcore"
|
|
|
|
"google.golang.org/grpc/grpclog"
|
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
zapLogger struct {
|
|
|
|
zapcore.Core
|
|
|
|
log *zap.SugaredLogger
|
|
|
|
}
|
|
|
|
|
2021-05-13 12:22:03 +00:00
|
|
|
// Logger includes grpclog.LoggerV2 interface with an additional
|
|
|
|
// Println method.
|
2021-03-30 22:46:33 +00:00
|
|
|
Logger interface {
|
|
|
|
grpclog.LoggerV2
|
|
|
|
Println(v ...interface{})
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2021-05-13 12:22:03 +00:00
|
|
|
// GRPC wraps given zap.Logger into grpclog.LoggerV2+ interface.
|
2021-03-30 22:46:33 +00:00
|
|
|
func GRPC(l *zap.Logger) Logger {
|
|
|
|
log := l.WithOptions(
|
|
|
|
// skip gRPCLog + zapLogger in caller
|
|
|
|
zap.AddCallerSkip(2))
|
|
|
|
|
|
|
|
return &zapLogger{
|
|
|
|
Core: log.Core(),
|
|
|
|
log: log.Sugar(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-13 12:22:03 +00:00
|
|
|
// Info implements grpclog.LoggerV2.
|
2021-03-30 22:46:33 +00:00
|
|
|
func (z *zapLogger) Info(args ...interface{}) { z.log.Info(args...) }
|
|
|
|
|
2021-05-13 12:22:03 +00:00
|
|
|
// Infoln implements grpclog.LoggerV2.
|
2021-03-30 22:46:33 +00:00
|
|
|
func (z *zapLogger) Infoln(args ...interface{}) { z.log.Info(args...) }
|
|
|
|
|
2021-05-13 12:22:03 +00:00
|
|
|
// Infof implements grpclog.LoggerV2.
|
2021-03-30 22:46:33 +00:00
|
|
|
func (z *zapLogger) Infof(format string, args ...interface{}) { z.log.Infof(format, args...) }
|
|
|
|
|
2021-05-13 12:22:03 +00:00
|
|
|
// Println allows to print a line with info severity.
|
2021-03-30 22:46:33 +00:00
|
|
|
func (z *zapLogger) Println(args ...interface{}) { z.log.Info(args...) }
|
|
|
|
|
2021-05-13 12:22:03 +00:00
|
|
|
// Printf implements grpclog.LoggerV2.
|
2021-03-30 22:46:33 +00:00
|
|
|
func (z *zapLogger) Printf(format string, args ...interface{}) { z.log.Infof(format, args...) }
|
|
|
|
|
2021-05-13 12:22:03 +00:00
|
|
|
// Warning implements grpclog.LoggerV2.
|
2021-03-30 22:46:33 +00:00
|
|
|
func (z *zapLogger) Warning(args ...interface{}) { z.log.Warn(args...) }
|
|
|
|
|
2021-05-13 12:22:03 +00:00
|
|
|
// Warningln implements grpclog.LoggerV2.
|
2021-03-30 22:46:33 +00:00
|
|
|
func (z *zapLogger) Warningln(args ...interface{}) { z.log.Warn(args...) }
|
|
|
|
|
2021-05-13 12:22:03 +00:00
|
|
|
// Warningf implements grpclog.LoggerV2.
|
2021-03-30 22:46:33 +00:00
|
|
|
func (z *zapLogger) Warningf(format string, args ...interface{}) { z.log.Warnf(format, args...) }
|
|
|
|
|
2021-05-13 12:22:03 +00:00
|
|
|
// Error implements grpclog.LoggerV2.
|
2021-03-30 22:46:33 +00:00
|
|
|
func (z *zapLogger) Error(args ...interface{}) { z.log.Error(args...) }
|
|
|
|
|
2021-05-13 12:22:03 +00:00
|
|
|
// Errorln implements grpclog.LoggerV2.
|
2021-03-30 22:46:33 +00:00
|
|
|
func (z *zapLogger) Errorln(args ...interface{}) { z.log.Error(args...) }
|
|
|
|
|
2021-05-13 12:22:03 +00:00
|
|
|
// Errorf implements grpclog.LoggerV2.
|
2021-03-30 22:46:33 +00:00
|
|
|
func (z *zapLogger) Errorf(format string, args ...interface{}) { z.log.Errorf(format, args...) }
|
|
|
|
|
2021-05-13 12:22:03 +00:00
|
|
|
// Fatal implements grpclog.LoggerV2.
|
2021-03-30 22:46:33 +00:00
|
|
|
func (z *zapLogger) Fatal(args ...interface{}) { z.log.Fatal(args...) }
|
|
|
|
|
2021-05-13 12:22:03 +00:00
|
|
|
// Fatalln implements grpclog.LoggerV2.
|
2021-03-30 22:46:33 +00:00
|
|
|
func (z *zapLogger) Fatalln(args ...interface{}) { z.log.Fatal(args...) }
|
|
|
|
|
2021-05-13 12:22:03 +00:00
|
|
|
// Fatalf implements grpclog.LoggerV2.
|
2021-04-29 08:58:36 +00:00
|
|
|
func (z *zapLogger) Fatalf(format string, args ...interface{}) { z.log.Fatalf(format, args...) }
|
2021-03-30 22:46:33 +00:00
|
|
|
|
2021-05-13 12:22:03 +00:00
|
|
|
// V implements grpclog.LoggerV2.
|
2021-03-30 22:46:33 +00:00
|
|
|
func (z *zapLogger) V(int) bool { return z.Enabled(zapcore.DebugLevel) }
|