2020-02-21 12:31:16 +03:00
|
|
|
package logrus
|
|
|
|
|
|
|
|
import (
|
2021-11-10 12:32:19 +03:00
|
|
|
"bytes"
|
2021-01-12 09:30:54 +03:00
|
|
|
"context"
|
2020-03-07 13:31:25 +03:00
|
|
|
"errors"
|
2020-02-21 12:31:16 +03:00
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/sirupsen/logrus"
|
2021-10-28 00:32:05 +03:00
|
|
|
"go.unistack.org/micro/v3/logger"
|
2020-02-21 12:31:16 +03:00
|
|
|
)
|
|
|
|
|
2022-01-20 00:33:48 +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 12:32:19 +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-21 12:31:16 +03:00
|
|
|
func TestName(t *testing.T) {
|
|
|
|
l := NewLogger()
|
2021-01-10 15:21:29 +03:00
|
|
|
if err := l.Init(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
l.V(logger.InfoLevel)
|
2020-02-21 12:31:16 +03:00
|
|
|
if l.String() != "logrus" {
|
|
|
|
t.Errorf("error: name expected 'logrus' actual: %s", l.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Logf("testing logger name: %s", l.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWithFields(t *testing.T) {
|
2021-01-19 23:33:56 +03:00
|
|
|
l := NewLogger(logger.WithOutput(os.Stdout))
|
|
|
|
|
2021-01-10 15:21:29 +03:00
|
|
|
if err := l.Init(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2020-03-07 13:31:25 +03:00
|
|
|
|
2021-08-06 02:31:49 +03:00
|
|
|
l = l.Fields("k1", "v1", "k2", 123456)
|
2021-01-19 23:33:56 +03:00
|
|
|
|
2020-03-07 13:31:25 +03:00
|
|
|
logger.DefaultLogger = l
|
2020-02-21 12:31:16 +03:00
|
|
|
|
2021-01-12 09:30:54 +03:00
|
|
|
logger.Info(context.TODO(), "testing: Info")
|
|
|
|
logger.Infof(context.TODO(), "testing: %s", "Infof")
|
2020-02-21 12:31:16 +03:00
|
|
|
}
|
|
|
|
|
2020-03-07 13:31:25 +03:00
|
|
|
func TestWithError(t *testing.T) {
|
2021-01-19 23:33:56 +03:00
|
|
|
l := NewLogger()
|
2021-01-10 15:21:29 +03:00
|
|
|
if err := l.Init(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2021-08-06 02:31:49 +03:00
|
|
|
l = l.Fields("error", errors.New("boom!"))
|
2021-01-19 23:33:56 +03:00
|
|
|
|
2020-03-07 13:31:25 +03:00
|
|
|
logger.DefaultLogger = l
|
|
|
|
|
2021-01-12 09:30:54 +03:00
|
|
|
logger.Error(context.TODO(), "testing: error")
|
2020-03-07 13:31:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestWithLogger(t *testing.T) {
|
|
|
|
// with *logrus.Logger
|
2021-01-19 23:33:56 +03:00
|
|
|
l := NewLogger(WithLogger(logrus.StandardLogger()))
|
2021-01-10 15:21:29 +03:00
|
|
|
if err := l.Init(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2021-08-06 02:31:49 +03:00
|
|
|
l = l.Fields("k1", "v1", "k2", 123456)
|
2021-01-19 23:33:56 +03:00
|
|
|
|
2020-03-07 13:31:25 +03:00
|
|
|
logger.DefaultLogger = l
|
2021-01-12 09:30:54 +03:00
|
|
|
logger.Info(context.TODO(), "testing: with *logrus.Logger")
|
2020-03-07 13:31:25 +03:00
|
|
|
|
|
|
|
// with *logrus.Entry
|
2021-01-19 23:33:56 +03:00
|
|
|
el := NewLogger(WithLogger(logrus.NewEntry(logrus.New())))
|
2021-01-10 15:21:29 +03:00
|
|
|
if err := el.Init(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2021-08-06 02:31:49 +03:00
|
|
|
l = l.Fields("k3", 3.456, "k4", true)
|
2021-01-19 23:33:56 +03:00
|
|
|
|
2020-03-07 13:31:25 +03:00
|
|
|
logger.DefaultLogger = el
|
2021-01-12 09:30:54 +03:00
|
|
|
logger.Info(context.TODO(), "testing: with *logrus.Entry")
|
2020-03-07 13:31:25 +03:00
|
|
|
}
|
|
|
|
|
2020-02-21 12:31:16 +03:00
|
|
|
func TestJSON(t *testing.T) {
|
2021-01-19 23:33:56 +03:00
|
|
|
l := NewLogger(WithJSONFormatter(&logrus.JSONFormatter{}))
|
|
|
|
if err := l.Init(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2020-02-21 12:31:16 +03:00
|
|
|
|
2021-01-19 23:33:56 +03:00
|
|
|
logger.DefaultLogger = l
|
2021-01-12 09:30:54 +03:00
|
|
|
logger.Infof(context.TODO(), "test logf: %s", "name")
|
2020-02-21 12:31:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSetLevel(t *testing.T) {
|
|
|
|
logger.DefaultLogger = NewLogger()
|
|
|
|
|
2021-01-10 15:21:29 +03:00
|
|
|
if err := logger.Init(logger.WithLevel(logger.DebugLevel)); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2021-01-12 09:30:54 +03:00
|
|
|
logger.Debugf(context.TODO(), "test show debug: %s", "debug msg")
|
2020-02-21 12:31:16 +03:00
|
|
|
|
2021-01-10 15:21:29 +03:00
|
|
|
if err := logger.Init(logger.WithLevel(logger.InfoLevel)); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2021-01-12 09:30:54 +03:00
|
|
|
logger.Debugf(context.TODO(), "test non-show debug: %s", "debug msg")
|
2020-02-21 12:31:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestWithReportCaller(t *testing.T) {
|
2021-01-10 15:21:29 +03:00
|
|
|
l := NewLogger(ReportCaller())
|
2020-02-21 12:31:16 +03:00
|
|
|
|
2021-01-10 15:21:29 +03:00
|
|
|
if err := l.Init(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
logger.DefaultLogger = l
|
2021-01-12 09:30:54 +03:00
|
|
|
logger.Infof(context.TODO(), "testing: %s", "WithReportCaller")
|
2020-02-21 12:31:16 +03:00
|
|
|
}
|