-
Notifications
You must be signed in to change notification settings - Fork 35
/
level_test.go
94 lines (81 loc) · 1.87 KB
/
level_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
package logutils
import (
"bytes"
"io"
"log"
"testing"
)
func TestLevelFilter_impl(t *testing.T) {
var _ io.Writer = new(LevelFilter)
}
func TestLevelFilter(t *testing.T) {
buf := new(bytes.Buffer)
filter := &LevelFilter{
Levels: []LogLevel{"DEBUG", "WARN", "ERROR"},
MinLevel: "WARN",
Writer: buf,
}
logger := log.New(filter, "", 0)
logger.Print("[WARN] foo")
logger.Println("[ERROR] bar")
logger.Println("[DEBUG] baz")
logger.Println("[WARN] buzz")
result := buf.String()
expected := "[WARN] foo\n[ERROR] bar\n[WARN] buzz\n"
if result != expected {
t.Fatalf("bad: %#v", result)
}
}
func TestLevelFilterCheck(t *testing.T) {
filter := &LevelFilter{
Levels: []LogLevel{"DEBUG", "WARN", "ERROR"},
MinLevel: "WARN",
Writer: nil,
}
testCases := []struct {
line string
check bool
}{
{"[WARN] foo\n", true},
{"[ERROR] bar\n", true},
{"[DEBUG] baz\n", false},
{"[WARN] buzz\n", true},
}
for _, testCase := range testCases {
result := filter.Check([]byte(testCase.line))
if result != testCase.check {
t.Errorf("Fail: %s", testCase.line)
}
}
}
func TestLevelFilter_SetMinLevel(t *testing.T) {
filter := &LevelFilter{
Levels: []LogLevel{"DEBUG", "WARN", "ERROR"},
MinLevel: "ERROR",
Writer: nil,
}
testCases := []struct {
line string
checkBefore bool
checkAfter bool
}{
{"[WARN] foo\n", false, true},
{"[ERROR] bar\n", true, true},
{"[DEBUG] baz\n", false, false},
{"[WARN] buzz\n", false, true},
}
for _, testCase := range testCases {
result := filter.Check([]byte(testCase.line))
if result != testCase.checkBefore {
t.Errorf("Fail: %s", testCase.line)
}
}
// Update the minimum level to WARN
filter.SetMinLevel("WARN")
for _, testCase := range testCases {
result := filter.Check([]byte(testCase.line))
if result != testCase.checkAfter {
t.Errorf("Fail: %s", testCase.line)
}
}
}