2020-02-09 19:46:33 +03:00
|
|
|
package zap
|
|
|
|
|
|
|
|
import (
|
2021-11-10 02:22:48 +03:00
|
|
|
"bytes"
|
2021-01-12 09:45:13 +03:00
|
|
|
"context"
|
2020-02-09 19:46:33 +03:00
|
|
|
"testing"
|
|
|
|
|
2021-01-10 15:41:04 +03:00
|
|
|
"go.uber.org/zap"
|
2021-10-28 00:47:01 +03:00
|
|
|
"go.unistack.org/micro/v3/logger"
|
2020-02-09 19:46:33 +03:00
|
|
|
)
|
|
|
|
|
2022-01-20 00:36:27 +03:00
|
|
|
func TestFields(t *testing.T) {
|
|
|
|
ctx := context.TODO()
|
|
|
|
buf := bytes.NewBuffer(nil)
|
|
|
|
l := NewLogger(logger.WithLevel(logger.TraceLevel), logger.WithOutput(buf))
|
|
|
|
if err := l.Init(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
l.Fields("key", "val").Info(ctx, "message")
|
|
|
|
if !bytes.Contains(buf.Bytes(), []byte(`"key":"val"`)) {
|
|
|
|
t.Fatalf("logger fields not works, buf contains: %s", buf.Bytes())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-10 02:22:48 +03:00
|
|
|
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())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-09 19:46:33 +03:00
|
|
|
func TestName(t *testing.T) {
|
2021-01-10 15:41:04 +03:00
|
|
|
l := NewLogger()
|
|
|
|
if err := l.Init(); err != nil {
|
2020-02-09 19:46:33 +03:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if l.String() != "zap" {
|
|
|
|
t.Errorf("name is error %s", l.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Logf("test logger name: %s", l.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLogf(t *testing.T) {
|
2021-01-10 15:41:04 +03:00
|
|
|
l := NewLogger()
|
|
|
|
if err := l.Init(); err != nil {
|
2020-02-09 19:46:33 +03:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2020-02-21 12:31:16 +03:00
|
|
|
logger.DefaultLogger = l
|
2021-01-12 09:45:13 +03:00
|
|
|
logger.Infof(context.TODO(), "test logf: %s", "name")
|
2020-02-09 19:46:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSetLevel(t *testing.T) {
|
2021-01-10 15:41:04 +03:00
|
|
|
l := NewLogger()
|
|
|
|
if err := l.Init(); err != nil {
|
2020-02-09 19:46:33 +03:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2021-01-10 15:41:04 +03:00
|
|
|
|
2020-02-21 12:31:16 +03:00
|
|
|
logger.DefaultLogger = l
|
2020-02-09 19:46:33 +03:00
|
|
|
|
2021-01-10 15:41:04 +03:00
|
|
|
if err := logger.Init(logger.WithLevel(logger.DebugLevel)); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2021-01-12 09:45:13 +03:00
|
|
|
l.Debugf(context.TODO(), "test show debug: %s", "debug msg")
|
2021-01-10 15:41:04 +03:00
|
|
|
|
|
|
|
if err := logger.Init(logger.WithLevel(logger.InfoLevel)); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2021-01-12 09:45:13 +03:00
|
|
|
l.Debugf(context.TODO(), "test non-show debug: %s", "debug msg")
|
2021-01-10 15:41:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2020-02-09 19:46:33 +03:00
|
|
|
|
2021-01-12 09:45:13 +03:00
|
|
|
logger.Infof(context.TODO(), "test logf: %s", "name")
|
2020-02-09 19:46:33 +03:00
|
|
|
}
|