-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlogger_test.go
119 lines (103 loc) · 2.23 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
package confucius
import (
"bytes"
"fmt"
"io"
"os"
"strings"
"testing"
"time"
)
func Test_defaultLogger(t *testing.T) {
l := defaultLogger()
if l.level != DebugLevel {
t.Fatal("default level should be DEBUG")
}
if l.output != io.Discard {
t.Fatal("default writer should be io.Discard")
}
if l.useCallback {
t.Fatal("should set default callback")
}
}
func Test_defaultCallback(t *testing.T) {
old := os.Stdout // keep backup of the real stdout
r, w, _ := os.Pipe()
os.Stdout = w
l := defaultLogger()
l.callback = defaultCallback(os.Stdout)
expected := fmt.Sprintf("%-7s %s message", DebugLevel, time.Now().Format("2006/01/02 15:04:05"))
l.Debug("message")
outC := make(chan string)
// copy the output in a separate goroutine so printing can't block indefinitely
go func() {
var buf bytes.Buffer
io.Copy(&buf, r)
outC <- buf.String()
}()
// back to normal state
w.Close()
os.Stdout = old // restoring the real stdout
actual := <-outC
if !strings.Contains(actual, expected) {
t.Fatalf("unexpected log entry: %+v", actual)
}
}
func Test_defaultCallback_Panic(t *testing.T) {
msg := "message"
defer func() {
if r := recover(); r != nil {
if r != msg {
t.Error("it should be throw panic")
}
}
}()
l := defaultLogger()
l.callback = defaultCallback(os.Stdout)
l.Panic(msg)
}
func Test_LogLevel_String(t *testing.T) {
level := LogLevel(999)
if level.String() != "UNKNOWN" {
t.Error("unexpected level string")
}
}
func Test_logger(t *testing.T) {
levels := []LogLevel{
DebugLevel,
TraceLevel,
InfoLevel,
WarningLevel,
ErrorLevel,
PanicLevel,
FatalLevel,
}
for _, level := range levels {
t.Run(level.String(), func(t *testing.T) {
l := defaultLogger()
var actual LogLevel
l.callback = func(level LogLevel, message, file string, line int) {
actual = level
}
switch level {
case DebugLevel:
l.Debug("message")
case TraceLevel:
l.Trace("message")
case InfoLevel:
l.Info("message")
case WarningLevel:
l.Warn("message")
case ErrorLevel:
l.Error("message")
case FatalLevel:
l.Fatal("message")
case PanicLevel:
l.Panic("message")
}
if actual != level {
t.Errorf("unexpected log entry: %+v", actual)
}
})
}
}