-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnxlog_hook_test.go
68 lines (59 loc) · 2.02 KB
/
nxlog_hook_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
package nxlog
import (
"bytes"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/sirupsen/logrus"
"reflect"
"time"
)
var _ = Describe("Nxlog hook", func() {
var (
entry *logrus.Entry
)
BeforeEach(func() {
logger := logrus.New()
logger.Out = &bytes.Buffer{}
entry = &logrus.Entry{
Time: time.Time{},
Level: logrus.InfoLevel,
Message: "test message",
Data: logrus.Fields{},
Logger: logger,
}
})
It("can be instantiated", func() {
hook, err := NewNxlogHook("tcp", "127.0.0.1:3000", map[string]interface{}{})
Expect(err).To(BeNil())
Expect(reflect.TypeOf(hook).String()).To(Equal("*nxlog.Hook"))
})
It("returns an error if connection fails", func() {
hook, err := NewNxlogHook("tcp", "127.0.0.1:7005", map[string]interface{}{})
Expect(err).ToNot(BeNil())
Expect(hook).To(BeNil())
})
It("allows the setting the minimum log level that will trigger this hook", func() {
hook, _ := NewNxlogHook("tcp", "127.0.0.1:3000", map[string]interface{}{})
hook.Level = logrus.FatalLevel
Expect(hook.Levels()).ToNot(ContainElement(logrus.InfoLevel))
Expect(hook.Levels()).ToNot(ContainElement(logrus.ErrorLevel))
hook.Level = logrus.ErrorLevel
Expect(hook.Levels()).ToNot(ContainElement(logrus.InfoLevel))
Expect(hook.Levels()).To(ContainElement(logrus.ErrorLevel))
hook.Level = logrus.DebugLevel
Expect(hook.Levels()).To(ContainElement(logrus.InfoLevel))
Expect(hook.Levels()).To(ContainElement(logrus.ErrorLevel))
})
It("allows formatting a message", func() {
hook, _ := NewNxlogHook("tcp", "127.0.0.1:3000", map[string]interface{}{})
By("using the default formatter", func() {
formatted, _ := hook.getMessage(entry)
Expect(formatted).To(Equal(`{"level":"info","msg":"test message","time":"0001-01-01T00:00:00Z"}` + "\n"))
})
By("using a text formatter", func() {
hook.Formatter = &logrus.TextFormatter{}
formatted, _ := hook.getMessage(entry)
Expect(formatted).To(Equal(`time="0001-01-01T00:00:00Z" level=info msg="test message"` + "\n"))
})
})
})