-
Notifications
You must be signed in to change notification settings - Fork 11
/
logger_test.go
86 lines (66 loc) · 1.65 KB
/
logger_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
82
83
84
85
86
package spice
import (
"bytes"
"errors"
"strings"
"testing"
"github.com/sirupsen/logrus"
)
func TestLoggerAdapt(t *testing.T) {
adapter := cleanLogger(t)
msg := "myInfoMessageString"
adapter.Info(msg)
if !strings.Contains(adapter.String(), "msg="+msg) || !strings.Contains(adapter.String(), "level=info") {
t.Error("did not find string")
}
}
func TestLoggerWithError(t *testing.T) {
adapter := cleanLogger(t)
msg := "myErrorMessageString"
error := errors.New(msg)
adapter.WithError(error).Info()
if !strings.Contains(adapter.String(), "error="+msg) {
t.Error("did not find short error message")
}
adapter = cleanLogger(t)
msg = "myErrorMessageString longer"
error = errors.New(msg)
adapter.WithError(error).Info()
if !strings.Contains(adapter.String(), "error=\""+msg+"\"") {
t.Error("did not find long error message")
}
}
func TestLoggerWithField(t *testing.T) {
adapter := cleanLogger(t)
field := "myField"
msg := "myFieldMessageString"
adapter.WithFields(field, msg).Info()
if !strings.Contains(adapter.String(), field+"="+msg) {
t.Error("did not find short field message")
}
adapter = cleanLogger(t)
field = "myField"
msg = "myFieldMessageString longer"
adapter.WithFields(field, msg).Info()
if !strings.Contains(adapter.String(), field+"=\""+msg+"\"") {
t.Error("did not find long field message")
}
}
func cleanLogger(t *testing.T) *clean {
t.Helper()
var buf bytes.Buffer
logger := logrus.New()
logger.Out = &buf
adapter := &clean{
Adapt(logger.WithField("logger", "clean")),
&buf,
}
return adapter
}
type clean struct {
Logger
buf *bytes.Buffer
}
func (c *clean) String() string {
return c.buf.String()
}