diff --git a/logger/grpc.go b/logger/grpc.go deleted file mode 100644 index 584bf25..0000000 --- a/logger/grpc.go +++ /dev/null @@ -1,78 +0,0 @@ -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 - } - - // Logger includes grpclog.LoggerV2 interface with an additional - // Println method. - Logger interface { - grpclog.LoggerV2 - Println(v ...interface{}) - } -) - -// GRPC wraps given zap.Logger into grpclog.LoggerV2+ interface. -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(), - } -} - -// Info implements grpclog.LoggerV2. -func (z *zapLogger) Info(args ...interface{}) { z.log.Info(args...) } - -// Infoln implements grpclog.LoggerV2. -func (z *zapLogger) Infoln(args ...interface{}) { z.log.Info(args...) } - -// Infof implements grpclog.LoggerV2. -func (z *zapLogger) Infof(format string, args ...interface{}) { z.log.Infof(format, args...) } - -// Println allows to print a line with info severity. -func (z *zapLogger) Println(args ...interface{}) { z.log.Info(args...) } - -// Printf implements grpclog.LoggerV2. -func (z *zapLogger) Printf(format string, args ...interface{}) { z.log.Infof(format, args...) } - -// Warning implements grpclog.LoggerV2. -func (z *zapLogger) Warning(args ...interface{}) { z.log.Warn(args...) } - -// Warningln implements grpclog.LoggerV2. -func (z *zapLogger) Warningln(args ...interface{}) { z.log.Warn(args...) } - -// Warningf implements grpclog.LoggerV2. -func (z *zapLogger) Warningf(format string, args ...interface{}) { z.log.Warnf(format, args...) } - -// Error implements grpclog.LoggerV2. -func (z *zapLogger) Error(args ...interface{}) { z.log.Error(args...) } - -// Errorln implements grpclog.LoggerV2. -func (z *zapLogger) Errorln(args ...interface{}) { z.log.Error(args...) } - -// Errorf implements grpclog.LoggerV2. -func (z *zapLogger) Errorf(format string, args ...interface{}) { z.log.Errorf(format, args...) } - -// Fatal implements grpclog.LoggerV2. -func (z *zapLogger) Fatal(args ...interface{}) { z.log.Fatal(args...) } - -// Fatalln implements grpclog.LoggerV2. -func (z *zapLogger) Fatalln(args ...interface{}) { z.log.Fatal(args...) } - -// Fatalf implements grpclog.LoggerV2. -func (z *zapLogger) Fatalf(format string, args ...interface{}) { z.log.Fatalf(format, args...) } - -// V implements grpclog.LoggerV2. -func (z *zapLogger) V(int) bool { return z.Enabled(zapcore.DebugLevel) } diff --git a/logger/option.go b/logger/option.go deleted file mode 100644 index f617010..0000000 --- a/logger/option.go +++ /dev/null @@ -1,33 +0,0 @@ -package logger - -import "go.uber.org/zap" - -// WithSamplingInitial returns Option that sets sampling initial parameter. -func WithSamplingInitial(v int) Option { return func(o *options) { o.SamplingInitial = v } } - -// WithSamplingThereafter returns Option that sets sampling thereafter parameter. -func WithSamplingThereafter(v int) Option { return func(o *options) { o.SamplingThereafter = v } } - -// WithFormat returns Option that sets format parameter. -func WithFormat(v string) Option { return func(o *options) { o.Format = v } } - -// WithLevel returns Option that sets Level parameter. -func WithLevel(v string) Option { return func(o *options) { o.Level = v } } - -// WithTraceLevel returns Option that sets trace level parameter. -func WithTraceLevel(v string) Option { return func(o *options) { o.TraceLevel = v } } - -// WithoutDisclaimer returns Option that disables disclaimer. -func WithoutDisclaimer() Option { return func(o *options) { o.NoDisclaimer = true } } - -// WithoutCaller returns Option that disables caller printing. -func WithoutCaller() Option { return func(o *options) { o.NoCaller = true } } - -// WithAppName returns Option that sets application name. -func WithAppName(v string) Option { return func(o *options) { o.AppName = v } } - -// WithAppVersion returns Option that sets application version. -func WithAppVersion(v string) Option { return func(o *options) { o.AppVersion = v } } - -// WithZapOptions returns Option that sets zap logger options. -func WithZapOptions(opts ...zap.Option) Option { return func(o *options) { o.Options = opts } } diff --git a/logger/zap.go b/logger/zap.go deleted file mode 100644 index 5e9ee62..0000000 --- a/logger/zap.go +++ /dev/null @@ -1,134 +0,0 @@ -package logger - -import ( - "strings" - - "go.uber.org/zap" - "go.uber.org/zap/zapcore" -) - -type ( - // Option represents logger option setter. - Option func(o *options) - - options struct { - Options []zap.Option - - SamplingInitial int - SamplingThereafter int - - Format string - Level string - TraceLevel string - - NoCaller bool - NoDisclaimer bool - - AppName string - AppVersion string - } -) - -const ( - formatJSON = "json" - formatConsole = "console" - - defaultSamplingInitial = 100 - defaultSamplingThereafter = 100 - - lvlInfo = "info" - lvlWarn = "warn" - lvlDebug = "debug" - lvlError = "error" - lvlFatal = "fatal" - lvlPanic = "panic" -) - -func safeLevel(lvl string) zap.AtomicLevel { - switch strings.ToLower(lvl) { - case lvlDebug: - return zap.NewAtomicLevelAt(zap.DebugLevel) - case lvlWarn: - return zap.NewAtomicLevelAt(zap.WarnLevel) - case lvlError: - return zap.NewAtomicLevelAt(zap.ErrorLevel) - case lvlFatal: - return zap.NewAtomicLevelAt(zap.FatalLevel) - case lvlPanic: - return zap.NewAtomicLevelAt(zap.PanicLevel) - default: - return zap.NewAtomicLevelAt(zap.InfoLevel) - } -} - -func defaults() *options { - return &options{ - SamplingInitial: defaultSamplingInitial, - SamplingThereafter: defaultSamplingThereafter, - - Format: formatConsole, - Level: lvlDebug, - TraceLevel: lvlInfo, - - NoCaller: false, - NoDisclaimer: false, - - AppName: "", - AppVersion: "", - } -} - -// New returns new zap.Logger using all options specified and stdout used -// for output. -func New(opts ...Option) (*zap.Logger, error) { - o := defaults() - c := zap.NewProductionConfig() - - c.OutputPaths = []string{"stdout"} - c.ErrorOutputPaths = []string{"stdout"} - - for _, opt := range opts { - opt(o) - } - - // set sampling - c.Sampling = &zap.SamplingConfig{ - Initial: o.SamplingInitial, - Thereafter: o.SamplingThereafter, - } - - // logger level - c.Level = safeLevel(o.Level) - traceLvl := safeLevel(o.TraceLevel) - - // logger format - switch f := o.Format; strings.ToLower(f) { - case formatConsole: - c.Encoding = formatConsole - default: - c.Encoding = formatJSON - } - - // logger time - c.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder - - if o.NoCaller { - c.EncoderConfig.EncodeCaller = nil - } - - // enable trace only for current log-level - o.Options = append(o.Options, zap.AddStacktrace(traceLvl)) - - l, err := c.Build(o.Options...) - if err != nil { - return nil, err - } - - if o.NoDisclaimer { - return l, nil - } - - return l.With( - zap.String("app_name", o.AppName), - zap.String("app_version", o.AppVersion)), nil -}