-
Notifications
You must be signed in to change notification settings - Fork 87
/
lfshook_test.go
49 lines (41 loc) · 1.25 KB
/
lfshook_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
package lfshook
import (
"bytes"
"github.com/sirupsen/logrus"
"io/ioutil"
"os"
"testing"
)
const expectedMsg = "This is the expected test message."
const unexpectedMsg = "This message should not be logged."
// Tests that writing to a tempfile log works.
// Matches the 'msg' of the output and deletes the tempfile.
func TestLogEntryWritten(t *testing.T) {
log := logrus.New()
// The colors were messing with the regexp so I turned them off.
tmpfile, err := ioutil.TempFile("", "test_lfshook")
if err != nil {
t.Errorf("Unable to generate logfile due to err: %s", err)
}
fname := tmpfile.Name()
defer func() {
tmpfile.Close()
os.Remove(fname)
}()
hook := NewHook(PathMap{
logrus.InfoLevel: fname,
}, nil)
log.Hooks.Add(hook)
log.Info(expectedMsg)
log.Warn(unexpectedMsg)
contents, err := ioutil.ReadAll(tmpfile)
if err != nil {
t.Errorf("Error while reading from tmpfile: %s", err)
}
if !bytes.Contains(contents, []byte("msg=\""+expectedMsg+"\"")) {
t.Errorf("Message read (%s) doesnt match message written (%s) for file: %s", contents, expectedMsg, fname)
}
if bytes.Contains(contents, []byte("msg=\""+unexpectedMsg+"\"")) {
t.Errorf("Message read (%s) contains message written (%s) for file: %s", contents, unexpectedMsg, fname)
}
}