This repository was archived by the owner on Jul 19, 2026. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathsource_test.go
More file actions
299 lines (230 loc) · 7.3 KB
/
Copy pathsource_test.go
File metadata and controls
299 lines (230 loc) · 7.3 KB
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
package logtic_test
import (
"bytes"
"fmt"
"os"
"path"
"regexp"
"testing"
"github.com/ecnepsnai/logtic"
)
// Test that the expected lines are printed to a log file
func TestSources(t *testing.T) {
Setup()
dir := t.TempDir()
logtic.Log.FilePath = path.Join(dir, "logtic.log")
logtic.Log.Level = logtic.LevelDebug
if err := logtic.Log.Open(); err != nil {
t.Fatalf("Error opening log file: %s", err.Error())
}
source := logtic.Log.Connect("test")
source.Write(logtic.LevelDebug, "this is a %s message", "debug")
debugPattern := regexp.MustCompile(`[0-9\-:TZ]+ \[DEBUG\]\[test\] this is a debug message`)
source.Write(logtic.LevelInfo, "this is an %s message", "info")
infoPattern := regexp.MustCompile(`[0-9\-:TZ]+ \[INFO\]\[test\] this is an info message`)
source.Write(logtic.LevelWarn, "this is a %s message", "warning")
warnPattern := regexp.MustCompile(`[0-9\-:TZ]+ \[WARN\]\[test\] this is a warning message`)
source.Write(logtic.LevelError, "this is an %s message", "error")
errorPattern := regexp.MustCompile(`[0-9\-:TZ]+ \[ERROR\]\[test\] this is an error message`)
logtic.Log.Close()
logFileData, err := os.ReadFile(path.Join(dir, "logtic.log"))
if err != nil {
panic(err)
}
if !debugPattern.Match(logFileData) {
t.Errorf("Log file does not contain expected log line for Debug message")
}
if !infoPattern.Match(logFileData) {
t.Errorf("Log file does not contain expected log line for Info message")
}
if !warnPattern.Match(logFileData) {
t.Errorf("Log file does not contain expected log line for Warn message")
}
if !errorPattern.Match(logFileData) {
t.Errorf("Log file does not contain expected log line for Error message")
}
if t.Failed() {
fmt.Printf("Log file data:\n%s\n", logFileData)
}
}
func TestPanicEvent(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Errorf("The code did not panic")
}
}()
Setup()
dir := t.TempDir()
logtic.Log.FilePath = path.Join(dir, "logtic.log")
logtic.Log.Level = logtic.LevelDebug
if err := logtic.Log.Open(); err != nil {
t.Fatalf("Error opening log file: %s", err.Error())
}
source := logtic.Log.Connect("test")
source.Panic("Ahh!")
}
func TestSourceLevel(t *testing.T) {
Setup()
dir := t.TempDir()
logtic.Log.FilePath = path.Join(dir, "logtic.log")
logtic.Log.Level = logtic.LevelDebug
if err := logtic.Log.Open(); err != nil {
t.Fatalf("Error opening log file: %s", err.Error())
}
source1 := logtic.Log.Connect("source1")
source2 := logtic.Log.Connect("source2")
source2.OverrideLevel(logtic.LevelWarn)
source1.Info("info message")
source2.Info("info message")
logtic.Log.Close()
logFileData, err := os.ReadFile(path.Join(dir, "logtic.log"))
if err != nil {
panic(err)
}
source1Pattern := regexp.MustCompile(`[0-9\-:TZ]+ \[INFO\]\[source1\] info message`)
source2Pattern := regexp.MustCompile(`[0-9\-:TZ]+ \[INFO\]\[source2\] info message`)
if !source1Pattern.Match(logFileData) {
t.Errorf("Log file does not contain expected log lines")
}
if source2Pattern.Match(logFileData) {
t.Errorf("Log file contains log line that should not exist")
}
}
func TestSourceWriteUnknownLevel(t *testing.T) {
Setup()
dir := t.TempDir()
logtic.Log.FilePath = path.Join(dir, "logtic.log")
logtic.Log.Level = logtic.LevelDebug
if err := logtic.Log.Open(); err != nil {
t.Fatalf("Error opening log file: %s", err.Error())
}
source := logtic.Log.Connect("test")
source.Write(9001, "What level is this?")
source.PWrite(9001, "Crazy!", nil)
}
func TestMultipleLogs(t *testing.T) {
Setup()
dir := t.TempDir()
aLogPath := path.Join(dir, "a.log")
bLogPath := path.Join(dir, "b.log")
logtic.Log.FilePath = aLogPath
logtic.Log.Level = logtic.LevelDebug
if err := logtic.Log.Open(); err != nil {
t.Fatalf("Error opening a log file: %s", err.Error())
}
aSource := logtic.Log.Connect("a-source")
bLog := logtic.New()
SetStdOut(bLog)
bLog.FilePath = bLogPath
bLog.Level = logtic.LevelDebug
if err := bLog.Open(); err != nil {
t.Fatalf("Error opening b log file: %s", err.Error())
}
bSource := bLog.Connect("b-source")
aSource.Info("Info")
bSource.Warn("Warn")
logtic.Log.Close()
bLog.Close()
aLogData, err := os.ReadFile(aLogPath)
if err != nil {
t.Fatalf("Error reading log file data: %s", err.Error())
}
bLogData, err := os.ReadFile(bLogPath)
if err != nil {
t.Fatalf("Error reading log file data: %s", err.Error())
}
infoPattern := regexp.MustCompile(`\[INFO\]\[[ab]-source\] Info`)
warnPattern := regexp.MustCompile(`\[WARN\]\[[ab]-source\] Warn`)
if !infoPattern.Match(aLogData) {
t.Errorf("Incorrect log data sent to source: info not found in A log")
}
if warnPattern.Match(aLogData) {
t.Errorf("Incorrect log data sent to source: warn found in A log")
}
if infoPattern.Match(bLogData) {
t.Errorf("Incorrect log data sent to source: info found in B log")
}
if !warnPattern.Match(bLogData) {
t.Errorf("Incorrect log data sent to source: warn not found in B log")
}
if t.Failed() {
t.Logf("a file: %s", aLogData)
t.Logf("b file: %s", bLogData)
}
}
func TestSourceEscapeCharacters(t *testing.T) {
Setup()
dir := t.TempDir()
logtic.Log.FilePath = path.Join(dir, "logtic.log")
logtic.Log.Level = logtic.LevelDebug
if err := logtic.Log.Open(); err != nil {
t.Fatalf("Error opening log file: %s", err.Error())
}
source := logtic.Log.Connect("test")
source.Write(logtic.LevelDebug, "Hello\n%s", "world")
debugPattern := regexp.MustCompile(`[0-9\-:TZ]+ \[DEBUG\]\[test\] Hello\\nworld`)
logtic.Log.Options.EscapeCharacters = false
source.Write(logtic.LevelInfo, "Hello\n%s", "world")
infoPattern := regexp.MustCompile(`[0-9\-:TZ]+ \[INFO\]\[test\] Hello\nworld`)
logtic.Log.Close()
logFileData, err := os.ReadFile(path.Join(dir, "logtic.log"))
if err != nil {
panic(err)
}
if !debugPattern.Match(logFileData) {
t.Errorf("Log file does not contain expected log line for Debug message")
}
if !infoPattern.Match(logFileData) {
t.Errorf("Log file does not contain expected log line for Info message")
}
if t.Failed() {
fmt.Printf("Log file data:\n%s\n", logFileData)
}
}
// Test that nothing is printed to the console when the logger is not opened
func TestLoggerNotOpened(t *testing.T) {
Setup()
b := &bytes.Buffer{}
logtic.Log.Stdout = b
logtic.Log.Stderr = b
source := logtic.Log.Connect("example")
source.Debug("Debug")
source.Info("Info")
source.Warn("Warn")
source.Error("Error")
if b.Len() != 0 {
t.Errorf("Data written to stdout/stderr that wasn't expected")
}
// Panic and Fatal messages are always printed to stderr
defer func() {
if r := recover(); r != nil {
Setup()
if b.Len() == 0 {
t.Errorf("Panic message not printed to stderr when expected")
}
}
}()
source.Panic("Panic!")
}
// Test that the instance does not panic (even if recovered) if a write event is called on a nil source or nil instance
func TestNilLogInstance(t *testing.T) {
origStderr := os.Stderr
tempStderr := path.Join(t.TempDir(), "stderr")
f, err := os.OpenFile(tempStderr, os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
panic(err)
}
os.Stderr = f
var log *logtic.Source
log.Error("Error")
f.Close()
data, err := os.ReadFile(tempStderr)
if err != nil {
panic(err)
}
if bytes.Contains(data, []byte("logtic: recovered from panic writing event. stack to follow")) {
t.Errorf("logtic instance did panic")
t.Logf("%s", data)
}
os.Stderr = origStderr
}