loggers updated to match to interface (#480)

refactor loggers to match go-micro changes
This commit is contained in:
Sumanth Chinthagunta 2020-02-21 01:31:16 -08:00 committed by Vasiliy Tolstov
parent c1cd04bb01
commit 6e079fd24f
4 changed files with 65 additions and 59 deletions

View File

@ -1,17 +0,0 @@
package zap
import (
"context"
"github.com/micro/go-micro/v2/logger"
)
// setOption returns a function to setup a context with given value
func setOption(k, v interface{}) logger.Option {
return func(o *logger.Options) {
if o.Context == nil {
o.Context = context.Background()
}
o.Context = context.WithValue(o.Context, k, v)
}
}

View File

@ -1,9 +1,10 @@
package zap package zap
import ( import (
"github.com/micro/go-micro/v2/logger"
"go.uber.org/zap" "go.uber.org/zap"
"go.uber.org/zap/zapcore" "go.uber.org/zap/zapcore"
"github.com/micro/go-micro/v2/logger"
) )
type Options struct { type Options struct {
@ -14,19 +15,18 @@ type configKey struct{}
// WithConfig pass zap.Config to logger // WithConfig pass zap.Config to logger
func WithConfig(c zap.Config) logger.Option { func WithConfig(c zap.Config) logger.Option {
return setOption(configKey{}, c) return logger.SetOption(configKey{}, c)
} }
type encoderConfigKey struct{} type encoderConfigKey struct{}
// WithEncoderConfig pass zapcore.EncoderConfig to logger // WithEncoderConfig pass zapcore.EncoderConfig to logger
func WithEncoderConfig(c zapcore.EncoderConfig) logger.Option { func WithEncoderConfig(c zapcore.EncoderConfig) logger.Option {
return setOption(encoderConfigKey{}, c) return logger.SetOption(encoderConfigKey{}, c)
} }
type levelKey struct{} type namespaceKey struct{}
// WithLevel pass log level func WithNamespace(namespace string) logger.Option {
func WithLevel(l logger.Level) logger.Option { return logger.SetOption(namespaceKey{}, namespace)
return setOption(levelKey{}, l)
} }

81
zap.go
View File

@ -3,54 +3,41 @@ package zap
import ( import (
"context" "context"
"fmt" "fmt"
"os"
"github.com/micro/go-micro/v2/logger"
"go.uber.org/zap" "go.uber.org/zap"
"go.uber.org/zap/zapcore" "go.uber.org/zap/zapcore"
"github.com/micro/go-micro/v2/logger"
) )
type zaplog struct { type zaplog struct {
cfg zap.Config cfg zap.Config
zap *zap.Logger zap *zap.Logger
} opts logger.Options
func (l *zaplog) Fields(fields map[string]interface{}) logger.Logger {
data := make([]zap.Field, len(fields))
for k, v := range fields {
data = append(data, zap.Any(k, v))
}
return &zaplog{cfg: l.cfg, zap: l.zap.With(data...)}
}
func (l *zaplog) Error(err error) logger.Logger {
return &zaplog{
cfg: l.cfg,
zap: l.zap.With(zap.Error(err)),
}
} }
func (l *zaplog) Init(opts ...logger.Option) error { func (l *zaplog) Init(opts ...logger.Option) error {
var err error var err error
options := &Options{logger.Options{Context: context.Background()}}
for _, o := range opts { for _, o := range opts {
o(&options.Options) o(&l.opts)
} }
zapConfig := zap.NewProductionConfig() zapConfig := zap.NewProductionConfig()
if zconfig, ok := options.Context.Value(configKey{}).(zap.Config); ok { if zconfig, ok := l.opts.Context.Value(configKey{}).(zap.Config); ok {
zapConfig = zconfig zapConfig = zconfig
} }
if zcconfig, ok := options.Context.Value(encoderConfigKey{}).(zapcore.EncoderConfig); ok { if zcconfig, ok := l.opts.Context.Value(encoderConfigKey{}).(zapcore.EncoderConfig); ok {
zapConfig.EncoderConfig = zcconfig zapConfig.EncoderConfig = zcconfig
} }
// Set log Level if not default
zapConfig.Level = zap.NewAtomicLevel() zapConfig.Level = zap.NewAtomicLevel()
if level, ok := options.Context.Value(levelKey{}).(logger.Level); ok { if l.opts.Level != logger.InfoLevel {
zapConfig.Level.SetLevel(loggerToZapLevel(level)) zapConfig.Level.SetLevel(loggerToZapLevel(l.opts.Level))
} }
log, err := zapConfig.Build() log, err := zapConfig.Build()
@ -58,23 +45,45 @@ func (l *zaplog) Init(opts ...logger.Option) error {
return err return err
} }
// Adding seed fields if exist
if l.opts.Fields != nil {
data := []zap.Field{}
for k, v := range l.opts.Fields {
data = append(data, zap.Any(k, v))
}
log = log.With(data...)
}
// Adding namespace
if namespace, ok := l.opts.Context.Value(namespaceKey{}).(string); ok {
log = log.With(zap.Namespace(namespace))
}
// defer log.Sync() ??
l.cfg = zapConfig l.cfg = zapConfig
l.zap = log l.zap = log
return nil return nil
} }
func (l *zaplog) SetLevel(level logger.Level) { func (l *zaplog) Fields(fields map[string]interface{}) logger.Logger {
l.cfg.Level.SetLevel(loggerToZapLevel(level)) data := make([]zap.Field, len(fields))
for k, v := range fields {
data = append(data, zap.Any(k, v))
}
l.zap = l.zap.With(data...)
return l
} }
func (l *zaplog) Level() logger.Level { func (l *zaplog) Error(err error) logger.Logger {
return zapToLoggerLevel(l.cfg.Level.Level()) l.zap = l.zap.With(zap.Error(err))
return l
} }
func (l *zaplog) Log(level logger.Level, args ...interface{}) { func (l *zaplog) Log(level logger.Level, args ...interface{}) {
lvl := loggerToZapLevel(level) lvl := loggerToZapLevel(level)
msg := fmt.Sprintf("%s", args) msg := fmt.Sprint(args...)
switch lvl { switch lvl {
case zap.DebugLevel: case zap.DebugLevel:
l.zap.Debug(msg) l.zap.Debug(msg)
@ -114,10 +123,22 @@ func (l *zaplog) String() string {
return "zap" return "zap"
} }
func (l *zaplog) Options() logger.Options {
return l.opts
}
// New builds a new logger based on options // New builds a new logger based on options
func NewLogger(opts ...logger.Option) (logger.Logger, error) { func NewLogger(opts ...logger.Option) (logger.Logger, error) {
l := &zaplog{} // Default options
if err := l.Init(); err != nil { options := logger.Options{
Level: logger.InfoLevel,
Fields: make(map[string]interface{}),
Out: os.Stderr,
Context: context.Background(),
}
l := &zaplog{opts: options}
if err := l.Init(opts...); err != nil {
return nil, err return nil, err
} }

View File

@ -25,7 +25,8 @@ func TestLogf(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
l.Logf(logger.InfoLevel, "test logf: %s", "name") logger.DefaultLogger = l
logger.Logf(logger.InfoLevel, "test logf: %s", "name")
} }
func TestSetLevel(t *testing.T) { func TestSetLevel(t *testing.T) {
@ -33,10 +34,11 @@ func TestSetLevel(t *testing.T) {
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
logger.DefaultLogger = l
l.SetLevel(logger.DebugLevel) logger.Init(logger.WithLevel(logger.DebugLevel))
l.Logf(logger.DebugLevel, "test show debug: %s", "debug msg") l.Logf(logger.DebugLevel, "test show debug: %s", "debug msg")
l.SetLevel(logger.InfoLevel) logger.Init(logger.WithLevel(logger.InfoLevel))
l.Logf(logger.DebugLevel, "test non-show debug: %s", "debug msg") l.Logf(logger.DebugLevel, "test non-show debug: %s", "debug msg")
} }