-
Notifications
You must be signed in to change notification settings - Fork 0
/
file_logger_test.go
120 lines (110 loc) · 2.85 KB
/
file_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
package log
import (
"sync"
"testing"
"time"
"github.com/no-src/log/formatter"
"github.com/no-src/log/level"
"github.com/no-src/log/option"
)
func TestFileLogger(t *testing.T) {
testCases := []struct {
name string
formatter string
concurrency bool
timeFormat string
}{
{"TextFormatter", formatter.TextFormatter, false, testTimeFormat},
{"JsonFormatter", formatter.JsonFormatter, false, testTimeFormat},
{"TextFormatter Concurrency", formatter.TextFormatter, true, ""},
{"JsonFormatter Concurrency", formatter.JsonFormatter, true, ""},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
fLogger, err := NewFileLogger(level.DebugLevel, "./logs", "ns_"+tc.formatter)
if err != nil {
t.Fatal(err)
}
InitDefaultLogger(fLogger.WithFormatter(formatter.New(tc.formatter)).WithTimeFormat(tc.timeFormat))
defer Close()
if tc.concurrency {
testLogsConcurrency(t, "TestFileLogger")
} else {
testLogs(t)
}
})
}
}
func TestFileLogger_WithAutoFlush(t *testing.T) {
wait := time.Second * 1
autoFlushFileLogger, err := NewFileLoggerWithAutoFlush(level.DebugLevel, "./logs", "ns", true, wait)
if err != nil {
t.Fatal(err)
}
InitDefaultLogger(autoFlushFileLogger)
testLogs(t)
<-time.After(wait + time.Second)
}
func TestFileLogger_WithAutoFlushWithCloseWhenWrite(t *testing.T) {
var wg sync.WaitGroup
wg.Add(1)
wait := time.Second * 1
autoFlushFileLogger, err := NewFileLoggerWithAutoFlush(level.DebugLevel, "./logs", "ns", true, wait)
if err != nil {
t.Fatal(err)
}
InitDefaultLogger(autoFlushFileLogger)
go func() {
Close()
wg.Done()
}()
testLogs(t)
// wait to close and stop the auto flush
<-time.After(wait + time.Second)
wg.Wait()
}
func TestFileLogger_WithAutoFlushWithFlushDelay(t *testing.T) {
wait := time.Millisecond * 10
autoFlushFileLogger, err := NewFileLoggerWithAutoFlush(level.DebugLevel, "./logs", "ns", true, wait)
if err != nil {
t.Fatal(err)
}
InitDefaultLogger(autoFlushFileLogger)
<-time.After(wait * 20)
}
func TestFileLogger_WithSplitDate(t *testing.T) {
fLogger, err := NewFileLoggerWithOption(option.FileLoggerOption{
Level: level.DebugLevel,
LogDir: "./split_date_logs",
FilePrefix: "ns",
SplitDate: true,
})
if err != nil {
t.Fatal(err)
}
InitDefaultLogger(fLogger)
defer Close()
testLogs(t)
}
func TestFileLogger_InitFileError(t *testing.T) {
_, err := NewFileLogger(level.DebugLevel, "/", "")
if err == nil {
t.Errorf("expect to get an error but get nil")
}
}
func TestFileLogger_WithMultiInitFile(t *testing.T) {
fLogger, err := NewFileLogger(level.DebugLevel, "./multi_init_file_logs", "ns")
if err != nil {
t.Fatal(err)
}
now = tomorrow
defer func() {
now = time.Now
}()
fLogger.(*fileLogger).initFile(true)
InitDefaultLogger(fLogger)
defer Close()
}
func tomorrow() time.Time {
return time.Now().Add(time.Hour * 24)
}