-
Notifications
You must be signed in to change notification settings - Fork 11
/
log_test.go
154 lines (130 loc) · 2.97 KB
/
log_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
package log
import (
"bytes"
"fmt"
"testing"
"time"
)
const uuid = "6ba7b814-9dad-11d1-80b4-00c04fd430c8"
func TestLogLevel(t *testing.T) {
l := GetLevel()
SetLevel(Linfo)
if IsDebugEnabled() || !IsInfoEnabled() || !IsWarnEnabled() {
t.FailNow()
}
SetLevel(l) // 恢复现场,避免影响其他单元测试
}
func TestSetWriter(t *testing.T) {
buf := bytes.NewBuffer(make([]byte, 4096))
SetWriter(buf)
rand := time.Now().String()
Info(rand)
if !bytes.Contains(buf.Bytes(), ([]byte)(rand)) {
t.FailNow()
}
}
func TestSetFormat(t *testing.T) {
format := fmt.Sprintf(`<log><date>%s</date><time>%s</time><level>%s</level><file>%s</file><line>%d</line><msg>%s</msg><log>`,
"2006-01-02", "15:04:05.000", LevelToken, PathToken, LineToken, MessageToken)
SetFormat(format)
buf := bytes.NewBuffer(make([]byte, 4096))
SetWriter(buf)
rand := time.Now().String()
Debug(rand)
if bytes.HasPrefix(buf.Bytes(), ([]byte)("<log><date>")) &&
!bytes.HasSuffix(buf.Bytes(), ([]byte)("</msg><log>")) {
t.FailNow()
}
}
func TestPanicLog(t *testing.T) {
defer func() {
if err := recover(); err == nil {
t.Fail()
}
}()
Panic("test panic")
}
func TestNormalLog(t *testing.T) {
SetLevel(Lall)
Trace(Lall)
Trace(Ltrace)
Debug(Ldebug)
Info(Linfo)
Warn(Lwarn)
Error(Lerror)
func() {
defer func() {
if err := recover(); err == nil {
t.Fail()
}
}()
Panic(Lpanic)
}()
// Fatal( LevelFatal)
Print(Lprint)
Stack(Lstack)
}
func TestFormatLog(t *testing.T) {
SetLevel(Lall)
Tracef("%d %s", Lall, Lall)
Tracef("%d %s", Ltrace, Ltrace)
Debugf("%d %s", Ldebug, Ldebug)
Infof("%d %s", Linfo, Linfo)
Warnf("%d %s", Lwarn, Lwarn)
Errorf("%d %s", Lerror, Lerror)
func() {
defer func() {
if err := recover(); err == nil {
t.Fail()
}
}()
Panicf("%d %s", Lpanic, Lpanic)
}()
// Fatalf("%d %s", Lfatal, Lfatal)
Printf("%d %s", Lprint, Lprint)
Stackf("%d %s", Lstack, Lstack)
}
func TestFormatLogWithTag(t *testing.T) {
format := "2006-01-02 15:04:05 tag info examples/main.go:88 message"
SetFormat(format)
SetLevel(Lall)
Tracef("%d %s", Lall, Lall)
Tracef("%d %s", Ltrace, Ltrace)
Debugf("%d %s", Ldebug, Ldebug)
Infof("%d %s", Linfo, Linfo)
Warnf("%d %s", Lwarn, Lwarn)
Errorf("%d %s", Lerror, Lerror)
func() {
defer func() {
if err := recover(); err == nil {
t.Fail()
}
}()
Panicf("%d %s", Lpanic, Lpanic)
}()
// Fatalf("%d %s", Lfatal, Lfatal)
Printf("%d %s", Lprint, Lprint)
Stackf("%d %s", Lstack, Lstack)
}
func TestCost(t *testing.T) {
defer Cost(666, "migo")()
time.Sleep(time.Second)
Info("do do do")
}
func TestCostf(t *testing.T) {
defer Costf("id=%d&name=%s", 666, "migo")()
time.Sleep(time.Second)
Info("do do do")
}
func TestTcost(t *testing.T) {
SetFormat(DefaultFormatTag)
defer Tcost("1001", 666, "migo")()
time.Sleep(time.Second)
Info("do do do")
}
func TestTcostf(t *testing.T) {
SetFormat(DefaultFormatTag)
defer Tcostf("1001", "id=%d&name=%s", 666, "migo")()
time.Sleep(time.Second)
Info("do do do")
}