-
Notifications
You must be signed in to change notification settings - Fork 10
/
logger_test.go
186 lines (138 loc) · 5.36 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
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package memfs_test
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"
. "github.com/bbengfort/memfs"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("Logger", func() {
Describe("log level handling", func() {
It("should be able to convert a log level to a string", func() {
Ω(LevelDebug.String()).Should(Equal("DEBUG"))
Ω(LevelInfo.String()).Should(Equal("INFO"))
Ω(LevelWarn.String()).Should(Equal("WARN"))
Ω(LevelError.String()).Should(Equal("ERROR"))
Ω(LevelFatal.String()).Should(Equal("FATAL"))
})
It("should be able to convert a string to a log level", func() {
Ω(LevelFromString("DEBUG")).Should(Equal(LevelDebug))
Ω(LevelFromString("INFO")).Should(Equal(LevelInfo))
Ω(LevelFromString("WARN")).Should(Equal(LevelWarn))
Ω(LevelFromString("WARNING")).Should(Equal(LevelWarn))
Ω(LevelFromString("ERROR")).Should(Equal(LevelError))
Ω(LevelFromString("FATAL")).Should(Equal(LevelFatal))
})
It("should convert any case string to a log level", func() {
Ω(LevelFromString("INFO")).Should(Equal(LevelInfo))
Ω(LevelFromString("info")).Should(Equal(LevelInfo))
Ω(LevelFromString("Info")).Should(Equal(LevelInfo))
Ω(LevelFromString("InFo")).Should(Equal(LevelInfo))
})
It("should convert strings with spaces to a log level", func() {
Ω(LevelFromString("ERROR ")).Should(Equal(LevelError))
Ω(LevelFromString("ERROR ")).Should(Equal(LevelError))
Ω(LevelFromString(" ERROR")).Should(Equal(LevelError))
Ω(LevelFromString(" ERROR ")).Should(Equal(LevelError))
})
})
Describe("logging methods", func() {
var (
err error // captured errors
testDir string // path to temporary test files
logger *Logger // logger instantiated from config
)
Context("to stdout", func() {
BeforeEach(func() {
logger, err = InitLogger("", "INFO")
Ω(err).Should(BeNil(), fmt.Sprintf("%s", err))
})
It("should output to os.Stdout", func() {
Ω(logger.GetHandler()).Should(Equal(os.Stdout))
})
It("should be able to set a new io.Writer for output", func() {
// Temporary Buffer
type Buffer struct {
bytes.Buffer
io.Closer
}
// Reset the logger handler
buf := new(Buffer)
logger.SetHandler(buf)
// Write a log message and test the output
logger.Log("test log message", LevelInfo)
logPattern := `INFO \[\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}[-+]\d{2}:\d{2}\]: test log message\n`
Ω(buf.String()).Should(MatchRegexp(logPattern))
})
})
Context("to a log file", func() {
var path string
BeforeEach(func() {
// Create a temporary log directory
testDir, err = ioutil.TempDir("", TempDirPrefix)
Ω(err).Should(BeNil(), fmt.Sprintf("%s", err))
// Create a config with the temporary path
path = filepath.Join(testDir, "testing.log")
// Initialize the logger
Ω(path).ShouldNot(BeAnExistingFile())
logger, err = InitLogger(path, "INFO")
Ω(err).Should(BeNil(), fmt.Sprintf("%s", err))
})
AfterEach(func() {
// Delete the temporary log file
err = os.RemoveAll(testDir)
Ω(err).Should(BeNil(), fmt.Sprintf("%s", err))
Ω(path).ShouldNot(BeAnExistingFile())
})
It("should output to an open file", func() {
Ω(path).Should(BeAnExistingFile())
})
It("should not log below the log level", func() {
Ω(logger.Level).Should(Equal(LevelInfo))
logger.Log("should not be logged", LevelDebug)
logger.Log("should be logged", LevelInfo)
logger.Log("definitely should be logged", LevelWarn)
data, err := ioutil.ReadFile(path)
Ω(err).Should(BeNil(), fmt.Sprintf("%s", err))
lines := strings.Split(string(data), "\n")
Ω(lines).Should(HaveLen(3))
Ω(lines[0]).Should(MatchRegexp(`INFO \[\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}[-+]\d{2}:\d{2}\]: should be logged`))
Ω(lines[1]).Should(MatchRegexp(`WARN \[\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}[-+]\d{2}:\d{2}\]: definitely should be logged`))
Ω(lines[2]).Should(BeZero())
})
It("should log debug messages", func() {
logger.Level = LevelDebug
logger.Debug("this is a debug message")
data, err := ioutil.ReadFile(path)
Ω(err).Should(BeNil(), fmt.Sprintf("%s", err))
Ω(string(data)).Should(MatchRegexp(`DEBUG \[\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}[-+]\d{2}:\d{2}\]: this is a debug message`))
})
It("should log info messages", func() {
logger.Info("for your information")
data, err := ioutil.ReadFile(path)
Ω(err).Should(BeNil(), fmt.Sprintf("%s", err))
Ω(string(data)).Should(MatchRegexp(`INFO \[\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}[-+]\d{2}:\d{2}\]: for your information`))
})
It("should log warning messages", func() {
logger.Warn("be careful!")
data, err := ioutil.ReadFile(path)
Ω(err).Should(BeNil(), fmt.Sprintf("%s", err))
Ω(string(data)).Should(MatchRegexp(`WARN \[\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}[-+]\d{2}:\d{2}\]: be careful!`))
})
It("should log error messages", func() {
logger.Error("there was a problem!")
data, err := ioutil.ReadFile(path)
Ω(err).Should(BeNil(), fmt.Sprintf("%s", err))
Ω(string(data)).Should(MatchRegexp(`ERROR \[\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}[-+]\d{2}:\d{2}\]: there was a problem!`))
})
It("should log fatal messages and exit", func() {
Skip("not sure how to check if fatal occurs")
})
})
})
})