2020-02-15 09:56:46 +03:00
|
|
|
package zerolog
|
2020-02-11 13:49:33 +03:00
|
|
|
|
|
|
|
import (
|
2021-11-10 12:23:56 +03:00
|
|
|
"bytes"
|
2021-01-12 09:53:50 +03:00
|
|
|
"context"
|
2020-02-15 09:56:46 +03:00
|
|
|
"errors"
|
2020-02-11 13:49:33 +03:00
|
|
|
"os"
|
|
|
|
"testing"
|
2020-02-15 09:56:46 +03:00
|
|
|
"time"
|
2020-02-11 13:49:33 +03:00
|
|
|
|
|
|
|
"github.com/rs/zerolog"
|
2021-10-28 00:52:30 +03:00
|
|
|
"go.unistack.org/micro/v3/logger"
|
2020-02-11 13:49:33 +03:00
|
|
|
)
|
|
|
|
|
2022-01-20 01:02:58 +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)
|
|
|
|
}
|
2022-02-22 14:17:16 +03:00
|
|
|
nl := l.Fields("key", "val")
|
|
|
|
nl.Infof(ctx, "message")
|
2022-01-20 01:02:58 +03:00
|
|
|
if !bytes.Contains(buf.Bytes(), []byte(`"key":"val"`)) {
|
|
|
|
t.Fatalf("logger fields not works, buf contains: %s", buf.Bytes())
|
|
|
|
}
|
2022-02-22 14:17:16 +03:00
|
|
|
buf.Reset()
|
2022-10-13 22:57:31 +03:00
|
|
|
mnl := nl.Fields("key1", "val1")
|
2022-02-22 14:17:16 +03:00
|
|
|
mnl.Infof(ctx, "message")
|
2022-10-13 22:57:31 +03:00
|
|
|
if !bytes.Contains(buf.Bytes(), []byte(`"key1":"val1"`)) || !bytes.Contains(buf.Bytes(), []byte(`"key":"val"`)) {
|
2022-02-22 14:17:16 +03:00
|
|
|
t.Fatalf("logger fields not works, buf contains: %s", buf.Bytes())
|
|
|
|
}
|
2022-01-20 01:02:58 +03:00
|
|
|
}
|
|
|
|
|
2021-11-10 12:23:56 +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-11 13:49:33 +03:00
|
|
|
func TestName(t *testing.T) {
|
|
|
|
l := NewLogger()
|
2021-01-12 10:23:17 +03:00
|
|
|
l.Init()
|
2020-02-11 13:49:33 +03:00
|
|
|
if l.String() != "zerolog" {
|
|
|
|
t.Errorf("error: name expected 'zerolog' actual: %s", l.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Logf("testing logger name: %s", l.String())
|
|
|
|
}
|
|
|
|
|
2020-02-21 12:31:16 +03:00
|
|
|
func TestWithOutput(t *testing.T) {
|
|
|
|
logger.DefaultLogger = NewLogger(logger.WithOutput(os.Stdout))
|
2021-01-12 10:23:17 +03:00
|
|
|
logger.DefaultLogger.Init()
|
2021-01-12 09:53:50 +03:00
|
|
|
logger.Infof(context.TODO(), "testing: %s", "WithOutput")
|
2020-02-21 12:31:16 +03:00
|
|
|
}
|
2020-02-11 13:49:33 +03:00
|
|
|
|
|
|
|
func TestSetLevel(t *testing.T) {
|
2020-02-21 12:31:16 +03:00
|
|
|
logger.DefaultLogger = NewLogger()
|
|
|
|
logger.Init(logger.WithLevel(logger.DebugLevel))
|
2021-01-12 09:53:50 +03:00
|
|
|
logger.Debugf(context.TODO(), "test show debug: %s", "debug msg")
|
2020-02-11 13:49:33 +03:00
|
|
|
|
2020-02-21 12:31:16 +03:00
|
|
|
logger.Init(logger.WithLevel(logger.InfoLevel))
|
2021-01-12 09:53:50 +03:00
|
|
|
logger.Debugf(context.TODO(), "test non-show debug: %s", "debug msg")
|
2020-02-11 13:49:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestWithReportCaller(t *testing.T) {
|
2020-02-21 12:31:16 +03:00
|
|
|
logger.DefaultLogger = NewLogger(ReportCaller())
|
2021-01-12 10:23:17 +03:00
|
|
|
logger.DefaultLogger.Init()
|
2021-01-12 09:53:50 +03:00
|
|
|
logger.Infof(context.TODO(), "testing: %s", "WithReportCaller")
|
2020-02-11 13:49:33 +03:00
|
|
|
}
|
2020-02-15 09:56:46 +03:00
|
|
|
|
2020-02-11 13:49:33 +03:00
|
|
|
func TestWithOut(t *testing.T) {
|
2020-02-21 12:31:16 +03:00
|
|
|
logger.DefaultLogger = NewLogger(logger.WithOutput(os.Stdout))
|
2021-01-12 10:23:17 +03:00
|
|
|
logger.DefaultLogger.Init()
|
2021-01-12 09:53:50 +03:00
|
|
|
logger.Infof(context.TODO(), "testing: %s", "WithOut")
|
2020-02-11 13:49:33 +03:00
|
|
|
}
|
|
|
|
|
2020-02-15 09:56:46 +03:00
|
|
|
func TestWithDevelopmentMode(t *testing.T) {
|
2020-02-21 12:31:16 +03:00
|
|
|
logger.DefaultLogger = NewLogger(WithDevelopmentMode(), WithTimeFormat(time.Kitchen))
|
2021-01-12 10:23:17 +03:00
|
|
|
logger.DefaultLogger.Init()
|
2021-01-12 09:53:50 +03:00
|
|
|
logger.Infof(context.TODO(), "testing: %s", "DevelopmentMode")
|
2020-02-11 13:49:33 +03:00
|
|
|
}
|
|
|
|
|
2020-02-15 09:56:46 +03:00
|
|
|
func TestWithFields(t *testing.T) {
|
2020-02-21 12:31:16 +03:00
|
|
|
logger.DefaultLogger = NewLogger()
|
2021-01-12 10:23:17 +03:00
|
|
|
logger.DefaultLogger.Init()
|
2021-08-06 02:50:15 +03:00
|
|
|
logger.Fields("sumo", "demo", "human", true, "age", 99).Infof(context.TODO(), "testing: %s", "WithFields")
|
2020-02-11 13:49:33 +03:00
|
|
|
}
|
|
|
|
|
2020-02-15 09:56:46 +03:00
|
|
|
func TestWithError(t *testing.T) {
|
2020-02-21 12:31:16 +03:00
|
|
|
logger.DefaultLogger = NewLogger()
|
2021-01-12 10:23:17 +03:00
|
|
|
logger.DefaultLogger.Init()
|
2021-08-06 02:50:15 +03:00
|
|
|
logger.Fields("error", errors.New("I am Error")).Errorf(context.TODO(), "testing: %s", "WithError")
|
2020-02-11 13:49:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestWithHooks(t *testing.T) {
|
|
|
|
simpleHook := zerolog.HookFunc(func(e *zerolog.Event, level zerolog.Level, msg string) {
|
|
|
|
e.Bool("has_level", level != zerolog.NoLevel)
|
|
|
|
e.Str("test", "logged")
|
|
|
|
})
|
|
|
|
|
2020-02-21 12:31:16 +03:00
|
|
|
logger.DefaultLogger = NewLogger(WithHooks([]zerolog.Hook{simpleHook}))
|
2021-01-12 10:23:17 +03:00
|
|
|
logger.DefaultLogger.Init()
|
2021-01-12 09:53:50 +03:00
|
|
|
logger.Infof(context.TODO(), "testing: %s", "WithHooks")
|
2020-02-11 13:49:33 +03:00
|
|
|
}
|