This repository has been archived by the owner on Sep 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlogger_test.go
151 lines (144 loc) · 3.84 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
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package logger
import (
"path"
"testing"
)
func TestGroup(t *testing.T) {
buf := New(nil)
l := buf.Log("").(*defaultLog)
l2 := buf.Log("").(*defaultLog)
if l.group != l2.group {
t.Errorf("ERROR: %d != %d for group %s", l.group, l2.group, l.Service())
} else {
t.Logf("%s: both have %d", l.Service(), l.group)
}
l3 := buf.Log("other").(*defaultLog)
if l.group == l3.group {
t.Errorf("ERROR: %s and %s have the same group %d", l.Service(), l3.Service(), l.group)
} else {
t.Logf("%s and %s have different groups %d and %d", l.Service(), l3.Service(), l.group, l3.group)
}
l4 := l3.Fork().(*defaultLog)
if l4.group == l3.group {
t.Errorf("ERROR: %s and %s have the same group %d", l4.Service(), l3.Service(), l4.group)
} else {
t.Logf("%s and %s have different groups %d and %d", l4.Service(), l3.Service(), l4.group, l3.group)
}
}
func expectLines(t *testing.T, count int, lines []*Line) {
t.Helper()
if len(lines) != count {
t.Errorf("ERROR: expected %d lines, not %d", count, len(lines))
} else {
t.Logf("Got expected number of lines: %d", count)
}
for i := range lines {
if lines[i].File != "logger_test.go" {
t.Errorf("Line trimmer did not trimm! Wanted logger_test.go, got %s", lines[i].File)
}
}
}
func TestSeq(t *testing.T) {
buf := New(nil)
buf.FileTrimmer(func(s string) string {
return path.Base(s)
})
l := buf.Log("")
l2 := buf.Log("")
l.Errorf("0")
l2.Errorf("1")
lines := buf.Lines(-1)
expectLines(t, 2, lines)
if lines[0].Group != lines[1].Group {
t.Errorf("ERROR: Expected lines to have the same group, not %d and %d", lines[0].Group, lines[1].Group)
} else {
t.Logf("Lines have the same group")
}
if lines[0].Seq == lines[1].Seq {
t.Errorf("ERROR: Lines have the same sequence %d, expected them to be different", lines[0].Seq)
} else {
t.Logf("Lines have different sequence numbers %d and %d", lines[0].Seq, lines[1].Seq)
}
}
func TestBuffer(t *testing.T) {
buf := New(nil)
buf.FileTrimmer(func(s string) string {
return path.Base(s)
})
l := buf.Log("")
if buf.MaxLines() != 1000 {
t.Errorf("ERROR: Expected MaxLines default to be 1000, not %d", buf.MaxLines())
} else {
t.Logf("Got expected MaxLines %d", buf.MaxLines())
}
l.Errorf("0")
l.Errorf("1")
lines := buf.Lines(-1)
expectLines(t, 2, lines)
buf.KeepLines(1)
if buf.MaxLines() != 1 {
t.Errorf("ERROR: Expected MaxLines to be 1, not %d", buf.MaxLines())
} else {
t.Logf("Got expected MaxLines %d", buf.MaxLines())
}
lines = buf.Lines(-1)
expectLines(t, 1, lines)
if lines[0].Message != "1" {
t.Errorf("Expected line message to be `1`, not `%s`", lines[0].Message)
} else {
t.Logf("Got expected line message `%s`", lines[0].Message)
}
l.Errorf("2")
lines = buf.Lines(-1)
expectLines(t, 1, lines)
if lines[0].Message != "2" {
t.Errorf("Expected line message to be `2`, not `%s`", lines[0].Message)
} else {
t.Logf("Got expected line message `%s`", lines[0].Message)
}
buf.KeepLines(2)
l.Errorf("3")
l.Errorf("4")
l.Errorf("5")
lines = buf.Lines(-1)
expectLines(t, 2, lines)
if lines[0].Message != "4" || lines[1].Message != "5" {
t.Errorf("Expected 4 and 5 for Lines, not `%s` and `%s`", lines[0].Message, lines[1].Message)
} else {
t.Logf("Got expected message contents `%s` and `%s`", lines[0].Message, lines[1].Message)
}
}
func logLevels(l Logger) {
l.Tracef("0")
l.Debugf("1")
l.Infof("2")
l.Warnf("3")
l.Errorf("4")
l.Fatalf("5")
l.Panicf("6")
l.Auditf("7")
}
func TestLevels(t *testing.T) {
lpl := map[Level]int{
Trace: 8,
Debug: 7,
Info: 6,
Warn: 5,
Error: 4,
Fatal: 3,
Panic: 2,
Audit: 1,
}
for lvl, count := range lpl {
buf := New(nil)
buf.FileTrimmer(func(s string) string {
return path.Base(s)
})
l := buf.Log("").(*defaultLog)
l.SetLevel(lvl)
logLevels(l)
lines := buf.Lines(-1)
t.Logf("Level %s, expect %d lines", lvl, count)
expectLines(t, count, lines)
}
}