-
Notifications
You must be signed in to change notification settings - Fork 0
/
decorate_test.go
128 lines (116 loc) · 2.66 KB
/
decorate_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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package golog
import (
"bytes"
"fmt"
"testing"
"time"
)
// Test that FilterLevel properly filter level less than specific.
func TestFilterLevel(t *testing.T) {
t.Parallel()
filterLevel := LevelWarn
kvs := []interface{}{"k1", "v1"}
tests := []struct {
name string
l Level
want string
}{
{
name: "DEBUG",
l: LevelDebug,
want: "",
},
{
name: "INFO",
l: LevelInfo,
want: "",
},
{
name: "WARN",
l: LevelWarn,
want: `WARN, "k1": "v1"` + "\n",
},
{
name: "ERROR",
l: LevelError,
want: `ERROR, "k1": "v1"` + "\n",
},
{
name: "FATAL",
l: LevelFatal,
want: `FATAL, "k1": "v1"` + "\n",
},
{
name: "other",
l: 10,
want: `10, "k1": "v1"` + "\n",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var buf bytes.Buffer
log := NewStdLogger(&buf)
log = WithFilter(log, FilterLevel(filterLevel))
log.Log(tt.l, kvs...)
if got := buf.String(); got != tt.want {
t.Errorf("buf.String() = %q want = %q", got, tt.want)
}
})
}
}
// Test that HandlerTimestamp properly append timestamp information into log.
func TestHandlerTimestamp(t *testing.T) {
t.Parallel()
now := time.Now()
nowFunc := func() time.Time {
return now
}
keyName := DefaultTimestampKeyName
valueFormat := DefaultTimestampFormat
tests := []struct {
name string
l Level
kvs []interface{}
want string
}{
{
name: "Without Log",
l: LevelInfo,
kvs: nil,
want: fmt.Sprintf(`INFO, "%s": "%s"`+"\n", keyName, now.Format(valueFormat)),
},
{
name: "With 1 Log",
l: LevelInfo,
kvs: []interface{}{"k1", 1},
want: fmt.Sprintf(`INFO, "k1": "1", "%s": "%s"`+"\n", keyName, now.Format(valueFormat)),
},
{
name: "With 2 Logs",
l: LevelInfo,
kvs: []interface{}{"k1", 1, "k2", "v2"},
want: fmt.Sprintf(`INFO, "k1": "1", "k2": "v2", "%s": "%s"`+"\n", keyName, now.Format(valueFormat)),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var buf bytes.Buffer
log := NewStdLogger(&buf)
log = WithHandler(log, HandlerTimestamp(keyName, valueFormat, nowFunc))
log.Log(tt.l, tt.kvs...)
if got := buf.String(); got != tt.want {
t.Errorf("buf.String() = %q want = %q", got, tt.want)
}
})
}
}
// Test that HandlerTimestamp properly append timestamp information into log.
func TestHandlerDefaultCaller(t *testing.T) {
var buf bytes.Buffer
log := NewStdLogger(&buf)
log = WithHandler(log, HandlerDefaultCaller)
log.Log(LevelInfo, "k1", "v1")
if got, want := buf.String(), `INFO, "k1": "v1", "caller": "decorate_test.go:124"`+"\n"; got != want {
t.Errorf("buf.String() = %q want = %q", got, want)
}
}