Skip to content

Commit

Permalink
allow to change output
Browse files Browse the repository at this point in the history
Signed-off-by: Vasiliy Tolstov <[email protected]>
  • Loading branch information
vtolstov committed Nov 9, 2021
1 parent 3b9eef8 commit b47f79d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
8 changes: 8 additions & 0 deletions zap.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
13 changes: 13 additions & 0 deletions zap_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,26 @@
package zap

import (
"bytes"
"context"
"testing"

"go.uber.org/zap"
"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 {
Expand Down

0 comments on commit b47f79d

Please sign in to comment.