allow to pass *zap.Logger via opts

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
Василий Толстов 2020-08-30 12:16:14 +03:00
parent 7f10d6dc20
commit 128bcf3b72
3 changed files with 40 additions and 7 deletions

View File

@ -24,6 +24,13 @@ func WithConfig(c zap.Config) logger.Option {
return logger.SetOption(configKey{}, c)
}
type loggerKey struct{}
// WithLogger pass *zap.Logger to logger
func WithLogger(l *zap.Logger) logger.Option {
return logger.SetOption(loggerKey{}, l)
}
type encoderConfigKey struct{}
// WithEncoderConfig pass zapcore.EncoderConfig to logger

16
zap.go
View File

@ -3,7 +3,6 @@ package zap
import (
"context"
"fmt"
"os"
"sync"
"go.uber.org/zap"
@ -13,7 +12,6 @@ import (
)
type zaplog struct {
cfg zap.Config
zap *zap.Logger
opts logger.Options
sync.RWMutex
@ -27,6 +25,11 @@ func (l *zaplog) Init(opts ...logger.Option) error {
o(&l.opts)
}
if zlog, ok := l.opts.Context.Value(loggerKey{}).(*zap.Logger); ok {
l.zap = zlog
return nil
}
zapConfig := zap.NewProductionConfig()
if zconfig, ok := l.opts.Context.Value(configKey{}).(zap.Config); ok {
zapConfig = zconfig
@ -69,7 +72,6 @@ func (l *zaplog) Init(opts ...logger.Option) error {
// defer log.Sync() ??
l.cfg = zapConfig
l.zap = log
l.fields = make(map[string]interface{})
@ -93,7 +95,6 @@ func (l *zaplog) Fields(fields map[string]interface{}) logger.Logger {
}
zl := &zaplog{
cfg: l.cfg,
zap: l.zap.With(data...),
opts: l.opts,
fields: make(map[string]interface{}),
@ -154,6 +155,10 @@ func (l *zaplog) Logf(level logger.Level, format string, args ...interface{}) {
}
}
func (l *zaplog) V(level logger.Level) bool {
return l.zap.Core().Enabled(loggerToZapLevel(level))
}
func (l *zaplog) String() string {
return "zap"
}
@ -168,7 +173,6 @@ func NewLogger(opts ...logger.Option) logger.Logger {
options := logger.Options{
Level: logger.InfoLevel,
Fields: make(map[string]interface{}),
Out: os.Stderr,
Context: context.Background(),
}
@ -193,6 +197,7 @@ func loggerToZapLevel(level logger.Level) zapcore.Level {
}
}
/*
func zapToLoggerLevel(level zapcore.Level) logger.Level {
switch level {
case zap.DebugLevel:
@ -209,3 +214,4 @@ func zapToLoggerLevel(level zapcore.Level) logger.Level {
return logger.InfoLevel
}
}
*/

View File

@ -4,6 +4,7 @@ import (
"testing"
"github.com/unistack-org/micro/v3/logger"
"go.uber.org/zap"
)
func TestName(t *testing.T) {
@ -37,9 +38,28 @@ func TestSetLevel(t *testing.T) {
logger.DefaultLogger = l
logger.Init(logger.WithLevel(logger.DebugLevel))
if err := logger.Init(logger.WithLevel(logger.DebugLevel)); err != nil {
t.Fatal(err)
}
l.Logf(logger.DebugLevel, "test show debug: %s", "debug msg")
logger.Init(logger.WithLevel(logger.InfoLevel))
if err := logger.Init(logger.WithLevel(logger.InfoLevel)); err != nil {
t.Fatal(err)
}
l.Logf(logger.DebugLevel, "test non-show debug: %s", "debug msg")
}
func TestWrapper(t *testing.T) {
z, err := zap.NewDevelopment()
if err != nil {
t.Fatal(err)
}
zl := NewLogger(WithLogger(z))
if err := zl.Init(); err != nil {
t.Fatal(err)
}
logger.DefaultLogger = zl
logger.Logf(logger.InfoLevel, "test logf: %s", "name")
}