From b47f79d55599ee4f17c507bb7abe23133f41d847 Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Wed, 10 Nov 2021 02:22:48 +0300 Subject: [PATCH] allow to change output Signed-off-by: Vasiliy Tolstov --- zap.go | 8 ++++++++ zap_test.go | 13 +++++++++++++ 2 files changed, 21 insertions(+) diff --git a/zap.go b/zap.go index 2a2748a..c1c707d 100644 --- a/zap.go +++ b/zap.go @@ -65,6 +65,14 @@ func (l *zaplog) Init(opts ...logger.Option) error { return err } + log = log.WithOptions(zap.WrapCore(func(c zapcore.Core) zapcore.Core { + return zapcore.NewCore( + zapcore.NewJSONEncoder(zapConfig.EncoderConfig), + zapcore.Lock(zapcore.AddSync(l.opts.Out)), + loggerToZapLevel(l.opts.Level), + ) + })) + // Adding seed fields if exist if l.opts.Fields != nil { data := make([]zap.Field, 0, len(l.opts.Fields)/2) diff --git a/zap_test.go b/zap_test.go index d947fef..ea3d3f7 100644 --- a/zap_test.go +++ b/zap_test.go @@ -1,6 +1,7 @@ package zap import ( + "bytes" "context" "testing" @@ -8,6 +9,18 @@ import ( "go.unistack.org/micro/v3/logger" ) +func TestOutput(t *testing.T) { + buf := bytes.NewBuffer(nil) + l := NewLogger(logger.WithOutput(buf)) + if err := l.Init(); err != nil { + t.Fatal(err) + } + l.Infof(context.TODO(), "test logger name: %s", "name") + if !bytes.Contains(buf.Bytes(), []byte(`test logger name`)) { + t.Fatalf("log not redirected: %s", buf.Bytes()) + } +} + func TestName(t *testing.T) { l := NewLogger() if err := l.Init(); err != nil {