2020-02-21 12:31:16 +03:00
|
|
|
package logrus
|
|
|
|
|
|
|
|
import (
|
2020-03-07 13:31:25 +03:00
|
|
|
"errors"
|
2020-02-21 12:31:16 +03:00
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/sirupsen/logrus"
|
2021-01-10 15:21:29 +03:00
|
|
|
"github.com/unistack-org/micro/v3/logger"
|
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) {
|
2020-03-07 13:31:25 +03:00
|
|
|
l := NewLogger(logger.WithOutput(os.Stdout)).Fields(map[string]interface{}{
|
|
|
|
"k1": "v1",
|
|
|
|
"k2": 123456,
|
|
|
|
})
|
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
|
|
|
|
|
|
|
logger.DefaultLogger = l
|
2020-02-21 12:31:16 +03:00
|
|
|
|
2021-01-10 15:21:29 +03:00
|
|
|
logger.Info("testing: Info")
|
|
|
|
logger.Infof("testing: %s", "Infof")
|
2020-02-21 12:31:16 +03:00
|
|
|
}
|
|
|
|
|
2020-03-07 13:31:25 +03:00
|
|
|
func TestWithError(t *testing.T) {
|
2020-03-18 00:57:10 +03:00
|
|
|
l := NewLogger().Fields(map[string]interface{}{"error": errors.New("boom!")})
|
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
|
|
|
logger.DefaultLogger = l
|
|
|
|
|
2021-01-10 15:21:29 +03:00
|
|
|
logger.Error("testing: error")
|
2020-03-07 13:31:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestWithLogger(t *testing.T) {
|
|
|
|
// with *logrus.Logger
|
|
|
|
l := NewLogger(WithLogger(logrus.StandardLogger())).Fields(map[string]interface{}{
|
|
|
|
"k1": "v1",
|
|
|
|
"k2": 123456,
|
|
|
|
})
|
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
|
|
|
logger.DefaultLogger = l
|
2021-01-10 15:21:29 +03:00
|
|
|
logger.Info("testing: with *logrus.Logger")
|
2020-03-07 13:31:25 +03:00
|
|
|
|
|
|
|
// with *logrus.Entry
|
2021-01-10 15:21:29 +03:00
|
|
|
el := NewLogger(WithLogger(logrus.NewEntry(logrus.New()))).Fields(map[string]interface{}{
|
2020-03-07 13:31:25 +03:00
|
|
|
"k3": 3.456,
|
|
|
|
"k4": true,
|
|
|
|
})
|
2021-01-10 15:21:29 +03:00
|
|
|
if err := el.Init(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2020-03-07 13:31:25 +03:00
|
|
|
logger.DefaultLogger = el
|
2021-01-10 15:21:29 +03:00
|
|
|
logger.Info("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) {
|
|
|
|
logger.DefaultLogger = NewLogger(WithJSONFormatter(&logrus.JSONFormatter{}))
|
|
|
|
|
2021-01-10 15:21:29 +03:00
|
|
|
logger.Infof("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)
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.Debugf("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)
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.Debugf("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
|
|
|
|
logger.Infof("testing: %s", "WithReportCaller")
|
2020-02-21 12:31:16 +03:00
|
|
|
}
|