-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlumberjackrus_test.go
140 lines (115 loc) · 3.13 KB
/
lumberjackrus_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
package lumberjackrus
import (
"testing"
"github.com/sirupsen/logrus"
"io/ioutil"
"bytes"
"os"
)
const expectedMsg = "Test message."
const unexpectedMsg = "Second test message"
const traceMsg = "Trace message"
const debugMsg = "Debug message"
const infoMsg = "Info message"
const errorMsg = "Error message"
const defaultFile = "/tmp/lumberjackrus_default.log"
const debugFile = "/tmp/lumberjackrus_debug.log"
const errorFile = "/tmp/lumberjackrus_error.log"
func initHookWithoutOption(formatter logrus.Formatter, minLevel logrus.Level) (*Hook, error) {
return NewHook(
&LogFile{
Filename: defaultFile,
MaxSize: 100,
MaxBackups: 1,
MaxAge: 1,
Compress: false,
},
minLevel,
formatter,
nil,
)
}
func initHookWithOption(formatter logrus.Formatter, minLevel logrus.Level) (*Hook, error) {
return NewHook(
&LogFile{
Filename: defaultFile,
MaxSize: 100,
MaxBackups: 1,
MaxAge: 1,
Compress: false,
},
minLevel,
formatter,
&LogFileOpts{
logrus.DebugLevel: &LogFile{Filename: debugFile},
logrus.ErrorLevel: &LogFile{Filename: errorFile},
},
)
}
func TestDefaultLogger(t *testing.T) {
defer func() {
os.Remove(defaultFile)
}()
// Init
minLevel := logrus.DebugLevel
formatter := &logrus.TextFormatter{}
log := logrus.New()
log.SetLevel(minLevel + 1)
hook, err := initHookWithoutOption(formatter, logrus.InfoLevel)
if err != nil {
t.Errorf("Unable to instantiate new hook: %s", err)
}
log.AddHook(hook)
// Run
log.Debug(unexpectedMsg)
log.Info(expectedMsg)
// Assertion
assertLogs(t, defaultFile, expectedMsg, unexpectedMsg)
}
func TestOptionsLogger(t *testing.T) {
defer func() {
os.Remove(defaultFile)
os.Remove(debugFile)
os.Remove(errorFile)
}()
// Init
minLevel := logrus.DebugLevel
formatter := &logrus.TextFormatter{}
log := logrus.New()
log.SetLevel(minLevel + 1)
hook, err := initHookWithOption(formatter, logrus.DebugLevel)
if err != nil {
t.Errorf("Unable to instantiate new hook: %s", err)
}
log.AddHook(hook)
// Run
log.Trace(traceMsg)
log.Debug(debugMsg)
log.Info(infoMsg)
log.Error(errorMsg)
// Assertion
assertLogs(t, defaultFile, infoMsg, traceMsg, debugMsg, errorMsg)
assertLogs(t, debugFile, debugMsg, traceMsg, infoMsg, errorMsg)
assertLogs(t, errorFile, errorMsg, traceMsg, debugMsg, infoMsg)
}
func assertLogs(t *testing.T, filename, expectedMessage string, unexpectedMessages ...string) {
logs, err := ioutil.ReadFile(filename)
if err != nil {
t.Errorf("Error while reading from log file: %s", err)
return
}
assertLogsContains(t, logs, filename, expectedMessage)
for _, unexpected := range unexpectedMessages {
assertLogsNotContains(t, logs, filename, unexpected)
}
}
func assertLogsContains(t *testing.T, logs []byte, filename, expected string) {
if !bytes.Contains(logs, []byte("msg=\""+expected+"\"")) {
t.Errorf("%s doesn't contain the expected message '%s'", filename, expected)
}
}
func assertLogsNotContains(t *testing.T, logs []byte, filename, unexpected string) {
if bytes.Contains(logs, []byte("msg=\""+unexpected+"\"")) {
t.Errorf("%s contains the unexpected message '%s'", filename, unexpected)
}
}