allow to change output
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
parent
3b9eef8d55
commit
b47f79d555
8
zap.go
8
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)
|
||||
|
13
zap_test.go
13
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 {
|
||||
|
Loading…
Reference in New Issue
Block a user