-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathlog_test.go
151 lines (141 loc) · 4.3 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
package log
import (
"fmt"
"github.com/stretchr/testify/assert"
"log"
"os"
"strconv"
"sync"
"testing"
"time"
)
func TestLogPrint(t *testing.T) {
t.Log("测试打印效果")
{
IsColor(false)
Warn("测试打印效果Warn")
Info("测试打印效果Info")
Debug("测试打印效果Info")
Trace("测试打印效果Trace")
Error("测试打印效果Error")
//Panic("测试打印效果Panic")
//Fatal("测试打印效果Fatal")
IsColor(true)
Trace("测试打印效果Trace")
Tracef("%s", "测试打印效果Tracef")
Info("测试打印效果Info")
Infof("%s", "测试打印效果Infof")
Debug("测试打印效果Info")
Debugf("%s", "测试打印效果Debugf")
Warn("测试打印效果Warn")
Warnf("%s", "测试打印效果Warnf")
Error("测试打印效果Error")
Errorf("%s", "测试打印效果Errorf")
SetLogDiscard(Levelwarn)
Warn("测试打印效果Warn")
Warnf("%s", "测试打印效果Warnf")
//Fatal("测试打印效果Fatal")
//Panic("测试打印效果Panic")
mylog := New()
mylog.Trace("mylog测试打印效果Trace")
mylog.Tracef("%s", "mylog测试打印效果Tracef")
mylog.Info("mylog测试打印效果Info")
mylog.Infof("%s", "mylog测试打印效果Infof")
mylog.Debug("mylog测试打印效果Info")
mylog.Debugf("%s", "mylog测试打印效果Debugf")
mylog.Warn("mylog测试打印效果Warn")
mylog.Warnf("%s", "mylog测试打印效果Warnf")
mylog.Error("mylog测试打印效果Error")
mylog.Errorf("%s", "mylog测试打印效果Errorf")
mylog2 := New()
mylog2.IsColor(true)
mylog2.SetLogDiscardLevel(Levelerror)
mylog2.Trace("mylog2测试打印效果Trace")
mylog2.Tracef("%s", "mylog2测试打印效果Tracef")
mylog2.Info("测试打印效果Info")
mylog2.Infof("%s", "mylog2测试打印效果Infof")
mylog2.Debug("测试打印效果Debug")
mylog2.Debugf("%s", "mylog2测试打印效果Debugf")
mylog2.Warn("测试打印效果Warn")
mylog2.Warnf("%s", "mylog2测试打印效果Warnf")
mylog2.Error("测试打印效果Error")
mylog2.Errorf("%s", "mylog2测试打印效果Errorf")
}
}
func TestLogMultithread(t *testing.T) {
t.Log("测试日志是否线程安全")
{
wait := sync.WaitGroup{}
wait.Add(200)
for i := 0; i < 100; i++ {
go func(i int) {
for j := 0; j <= 10; j++ {
Tracef("1试试可以正常打印吗!这是第%d次 第%d次!", i, j)
}
wait.Done()
}(i)
}
for i := 0; i < 100; i++ {
go func(i int) {
for j := 0; j <= 10; j++ {
Tracef("2试试可以正常打印吗!这是第%d次 第%d次!", i, j)
}
wait.Done()
}(i)
}
wait.Wait()
}
}
func TestMapVSArray(t *testing.T) {
t.Log("测试字典与切片,使用哪个性能更好")
count := 1000000
loggerMap := map[logType]*log.Logger{
Leveltrace: log.New(os.Stdout, "", 0),
Levelinfo: log.New(os.Stdout, "", 0),
Leveldebug: log.New(os.Stdout, "", 0),
Levelwarn: log.New(os.Stderr, "", 0),
Levelerror: log.New(os.Stderr, "", 0),
Levelpanic: log.New(os.Stderr, "", 0),
Levelfatal: log.New(os.Stderr, "", 0),
}
loggerArr := []*log.Logger{
Leveltrace: log.New(os.Stdout, "", 0),
Levelinfo: log.New(os.Stdout, "", 0),
Leveldebug: log.New(os.Stdout, "", 0),
Levelwarn: log.New(os.Stderr, "", 0),
Levelerror: log.New(os.Stderr, "", 0),
Levelpanic: log.New(os.Stderr, "", 0),
Levelfatal: log.New(os.Stderr, "", 0),
}
fmt.Println("---------------count:", count)
st := time.Now()
for i := 0; i < count; i++ {
assert.NotNil(t, loggerMap[Leveltrace])
assert.NotNil(t, loggerMap[Levelinfo])
assert.NotNil(t, loggerMap[Leveldebug])
assert.NotNil(t, loggerMap[Levelwarn])
assert.NotNil(t, loggerMap[Levelerror])
assert.NotNil(t, loggerMap[Levelpanic])
assert.NotNil(t, loggerMap[Levelfatal])
}
fmt.Println("map:", time.Since(st))
st = time.Now()
for i := 0; i < count; i++ {
assert.NotNil(t, loggerArr[Leveltrace])
assert.NotNil(t, loggerArr[Levelinfo])
assert.NotNil(t, loggerArr[Leveldebug])
assert.NotNil(t, loggerArr[Levelwarn])
assert.NotNil(t, loggerArr[Levelerror])
assert.NotNil(t, loggerArr[Levelpanic])
assert.NotNil(t, loggerArr[Levelfatal])
}
fmt.Println("array:", time.Since(st))
}
func TestColorCode(t *testing.T) {
t.Log("测试颜色代码")
{
for i := 30; i <= 40; i++ {
fmt.Printf("\033[%sm%s\033[0m\n", strconv.Itoa(i), "some thins you want to print out.")
}
}
}