-
Notifications
You must be signed in to change notification settings - Fork 7
/
logger_test.go
158 lines (144 loc) · 4.08 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
152
153
154
155
156
157
158
package log
import (
"bytes"
"testing"
)
var (
logHits int
exitHits int
)
type hitsFormatter struct{}
func (f *hitsFormatter) Format(level Level, msg string, logger *Logger) []byte {
logHits++
return []byte(msg)
}
func init() {
Default.Formatter = new(hitsFormatter)
Default.Out = new(bytes.Buffer)
Exit = func(code int) {
exitHits++
}
}
func TestLogOnLevel(t *testing.T) {
type fns struct {
fn0 func() bool
fn1 func(...interface{})
fn2 func(...interface{})
fn3 func(string, ...interface{})
ispanic bool
isexit bool
}
debugFns := fns{IsDebugEnabled, Debug, Debugln, Debugf, false, false}
infoFns := fns{IsInfoEnabled, Info, Infoln, Infof, false, false}
printFns := fns{IsPrintEnabled, Print, Println, Printf, false, false}
warnFns := fns{IsWarnEnabled, Warn, Warnln, Warnf, false, false}
errorFns := fns{IsErrorEnabled, Error, Errorln, Errorf, false, false}
panicFns := fns{IsPanicEnabled, Panic, Panicln, Panicf, true, false}
fatalFns := fns{IsFatalEnabled, Fatal, Fatalln, Fatalf, false, true}
tests := []struct {
level Level
fns fns
hits int
}{
// 0
{level: DEBUG, fns: debugFns, hits: 1},
{level: DEBUG, fns: infoFns, hits: 1},
{level: DEBUG, fns: printFns, hits: 1},
{level: DEBUG, fns: warnFns, hits: 1},
{level: DEBUG, fns: errorFns, hits: 1},
{level: DEBUG, fns: panicFns, hits: 1},
{level: DEBUG, fns: fatalFns, hits: 1},
// 7
{level: INFO, fns: debugFns, hits: 0},
{level: INFO, fns: infoFns, hits: 1},
{level: INFO, fns: printFns, hits: 1},
{level: INFO, fns: warnFns, hits: 1},
{level: INFO, fns: errorFns, hits: 1},
{level: INFO, fns: panicFns, hits: 1},
{level: INFO, fns: fatalFns, hits: 1},
// 14
{level: WARN, fns: debugFns, hits: 0},
{level: WARN, fns: infoFns, hits: 0},
{level: WARN, fns: printFns, hits: 1},
{level: WARN, fns: warnFns, hits: 1},
{level: WARN, fns: errorFns, hits: 1},
{level: WARN, fns: panicFns, hits: 1},
{level: WARN, fns: fatalFns, hits: 1},
// 21
{level: ERROR, fns: debugFns, hits: 0},
{level: ERROR, fns: infoFns, hits: 0},
{level: ERROR, fns: printFns, hits: 1},
{level: ERROR, fns: warnFns, hits: 0},
{level: ERROR, fns: errorFns, hits: 1},
{level: ERROR, fns: panicFns, hits: 1},
{level: ERROR, fns: fatalFns, hits: 1},
// 28
{level: PANIC, fns: debugFns, hits: 0},
{level: PANIC, fns: infoFns, hits: 0},
{level: PANIC, fns: printFns, hits: 1},
{level: PANIC, fns: warnFns, hits: 0},
{level: PANIC, fns: errorFns, hits: 0},
{level: PANIC, fns: panicFns, hits: 1},
{level: PANIC, fns: fatalFns, hits: 1},
// 35
{level: FATAL, fns: debugFns, hits: 0},
{level: FATAL, fns: infoFns, hits: 0},
{level: FATAL, fns: printFns, hits: 1},
{level: FATAL, fns: warnFns, hits: 0},
{level: FATAL, fns: errorFns, hits: 0},
{level: FATAL, fns: panicFns, hits: 0},
{level: FATAL, fns: fatalFns, hits: 1},
// 42
{level: OFF, fns: debugFns, hits: 0},
{level: OFF, fns: infoFns, hits: 0},
{level: OFF, fns: printFns, hits: 0},
{level: OFF, fns: warnFns, hits: 0},
{level: OFF, fns: errorFns, hits: 0},
{level: OFF, fns: panicFns, hits: 0},
{level: OFF, fns: fatalFns, hits: 0},
}
for i, tt := range tests {
func() {
defer func() {
// check panic hits
if err := recover(); err != nil {
if !tt.fns.ispanic {
t.Errorf("Case %d, got panic", i)
}
} else {
if tt.fns.ispanic {
t.Errorf("Case %d, no panic found", i)
}
}
}()
// reset
logHits = 0
exitHits = 0
Default.Level = tt.level
// run
tt.fns.fn1("message")
tt.fns.fn2("message")
tt.fns.fn3("message")
// check is enabled
if tt.fns.fn0() {
if logHits == 0 {
t.Errorf("Case %d, IsEnabled, but no log hits", i)
}
} else {
if logHits != 0 {
t.Errorf("Case %d, not IsEnabled, but found log hits=%v", i, logHits)
}
}
// check log hits
if logHits != tt.hits*3 {
t.Errorf("Case %d, fn hits on level %v, got %v, want %v", i, tt.level, logHits, tt.hits)
}
// check exit hits
if tt.fns.isexit {
if exitHits != 3 {
t.Errorf("Case %d, no exits hits", i)
}
}
}()
}
}