-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger_test.go
108 lines (92 loc) · 2.45 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
package log
import (
"errors"
"testing"
"time"
"github.com/no-src/log/content"
"github.com/no-src/log/formatter"
"github.com/no-src/log/internal/sync"
"github.com/no-src/log/level"
)
var (
concurrencyCount = 3
concurrencyTimeout = time.Second * 5
testTimeFormat = time.RFC3339
)
func testLogs(t *testing.T) {
DefaultLogger().WithFormatter(formatter.Default()).WithTimeFormat(content.DefaultLogTimeFormat())
Debug("%s %s, test debug log", "hello", "world")
Info("%s %s, test info log", "hello", "world")
Warn("%s %s, test warn log", "hello", "world")
Error(errors.New("log err"), "%s %s,test error log", "hello", "world")
ErrorIf(errors.New("log err from ErrorIf"), "%s %s, test error log", "hello", "world")
ErrorIf(nil, "%s %s, this error log will not be printed", "hello", "world")
testSampleLogs()
Log("%s %s, test log log", "hello", "world")
Log("%s %s, test log log again", "hello", "world")
DefaultLogger().Write([]byte(""))
DefaultLogger().Write([]byte("hello logger"))
}
func testLogsConcurrency(t *testing.T, testName string) {
wg := sync.WaitGroup{}
wg.Add(concurrencyCount)
for i := 0; i < concurrencyCount; i++ {
go func() {
testLogs(t)
wg.Done()
}()
}
if wg.WaitWithTimeout(concurrencyTimeout) {
t.Errorf("[concurrency] %s timeout for %s", testName, concurrencyTimeout.String())
}
}
func TestDefaultLogger(t *testing.T) {
defer Close()
testLogs(t)
}
func TestDefaultLogger_Concurrency(t *testing.T) {
defer Close()
testLogsConcurrency(t, "TestDefaultLogger_Concurrency")
}
func TestMinLogLevel(t *testing.T) {
InitDefaultLogger(NewConsoleLogger(level.InfoLevel))
defer Close()
testLogs(t)
}
func TestNilLogger(t *testing.T) {
InitDefaultLogger(nil)
defer Close()
testLogs(t)
}
func TestBaseLogger_Close(t *testing.T) {
InitDefaultLogger(newMinLogger())
// call baseLogger.Close
defer Close()
testLogs(t)
}
func TestReadWriteLoggerConcurrency(t *testing.T) {
go func() {
for i := 0; i < 10; i++ {
InitDefaultLogger(NewConsoleLogger(level.DebugLevel))
}
}()
testLogsConcurrency(t, "TestReadWriteLoggerConcurrency")
defer Close()
}
type minLogger struct {
baseLogger
}
func newMinLogger() Logger {
logger := &minLogger{}
logger.init(logger, level.DebugLevel, true)
return logger
}
func (l *minLogger) Write(p []byte) (n int, err error) {
return len(p), nil
}
func (l *minLogger) WithFormatter(f formatter.Formatter) Logger {
return l
}
func (l *minLogger) WithTimeFormat(f string) Logger {
return l
}