forked from birkirb/loggers-mapper-logrus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logrus_test.go
81 lines (65 loc) · 2.21 KB
/
logrus_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package logrus
import (
"bytes"
"regexp"
"testing"
"github.com/Vivino/go-loggers"
"github.com/sirupsen/logrus"
)
func TestLogrusInterface(t *testing.T) {
var _ loggers.Contextual = NewDefaultLogger()
var _ loggers.Advanced = &logrus.Logger{}
}
func TestLogrusLevelOutput(t *testing.T) {
l, b := newBufferedLogrusLog()
l.Info("This is a test")
expectedMatch := "(?i)info.*This is a test"
actual := b.String()
if ok, _ := regexp.Match(expectedMatch, []byte(actual)); !ok {
t.Errorf("Log output mismatch %s (actual) != %s (expected)", actual, expectedMatch)
}
}
func TestLogrusLevelfOutput(t *testing.T) {
l, b := newBufferedLogrusLog()
l.Errorf("This is %s test", "a")
expectedMatch := "(?i)erro.*This is a test"
actual := b.String()
if ok, _ := regexp.Match(expectedMatch, []byte(actual)); !ok {
t.Errorf("Log output mismatch %s (actual) != %s (expected)", actual, expectedMatch)
}
}
func TestLogrusLevellnOutput(t *testing.T) {
l, b := newBufferedLogrusLog()
l.Debugln("This is a test.", "So is this.")
expectedMatch := "(?i)debu.*This is a test. So is this."
actual := b.String()
if ok, _ := regexp.Match(expectedMatch, []byte(actual)); !ok {
t.Errorf("Log output mismatch %s (actual) != %s (expected)", actual, expectedMatch)
}
}
func TestLogrusWithFieldsOutput(t *testing.T) {
l, b := newBufferedLogrusLog()
l.WithFields("test", true).Warn("This is a message.")
expectedMatch := "(?i)warn.*This is a message.*test.*=true"
actual := b.String()
if ok, _ := regexp.Match(expectedMatch, []byte(actual)); !ok {
t.Errorf("Log output mismatch %s (actual) != %s (expected)", actual, expectedMatch)
}
}
func TestLogrusWithFieldsfOutput(t *testing.T) {
l, b := newBufferedLogrusLog()
l.WithFields("test", true, "Error", "serious").Errorf("This is a %s.", "message")
expectedMatch := "(?i)erro.*This is a message.*Error.*=serious.*test.*=true"
actual := b.String()
if ok, _ := regexp.Match(expectedMatch, []byte(actual)); !ok {
t.Errorf("Log output mismatch %s (actual) != %s (expected)", actual, expectedMatch)
}
}
func newBufferedLogrusLog() (loggers.Contextual, *bytes.Buffer) {
var b []byte
var bb = bytes.NewBuffer(b)
l := logrus.New()
l.Out = bb
l.Level = logrus.DebugLevel
return NewLogger(l), bb
}