allow to change output

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
Василий Толстов 2021-11-10 02:22:48 +03:00
parent 3b9eef8d55
commit b47f79d555
2 changed files with 21 additions and 0 deletions

8
zap.go
View File

@ -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)

View File

@ -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 {