-
Notifications
You must be signed in to change notification settings - Fork 13
/
prod_formatter_test.go
153 lines (127 loc) · 3.94 KB
/
prod_formatter_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
package logrus_cloudwatchlogs
import (
"bytes"
"encoding/json"
"errors"
"io/ioutil"
"net/http"
"testing"
"github.com/kdar/gtest"
"github.com/sirupsen/logrus"
"github.com/smartystreets/assertions/should"
)
//Example is a struct that implements Marshaler. It marshals as a map where "Content" is set to the value of A, and B is discarded
type Example struct {
A, B string
}
func (e Example) MarshalLog() map[string]interface{} {
out := make(map[string]interface{})
out["Content"] = e.A
return out
}
func TestProdFormatter(t *testing.T) {
a := gtest.New(t)
l := logrus.New()
l.Out = ioutil.Discard
prodFormatter := NewProdFormatter()
l.Formatter = prodFormatter
buf := &bytes.Buffer{}
l.Hooks.Add(NewWriterHook(buf))
l.WithFields(logrus.Fields{
"event": "testevent",
"topic": "testtopic",
"key": "testkey",
"marshaltest": Example{A: "hello", B: "this gets dropped"},
}).Info("Some event")
// split := bytes.SplitN(buf.Bytes(), []byte(" "), 2)
// a.So(len(split), should.Equal, 2).ElseFatal()
var v map[string]interface{}
err := json.Unmarshal(buf.Bytes(), &v)
a.So(err, should.BeNil).ElseFatal()
a.So(v["event"], should.Equal, "testevent").ElseFatal()
a.So(v["topic"], should.Equal, "testtopic").ElseFatal()
a.So(v["key"], should.Equal, "testkey").ElseFatal()
a.So(v["msg"], should.Equal, "Some event").ElseFatal()
a.So(v["marshaltest"].(map[string]interface{})["Content"], should.Equal, "hello").ElseFatal()
}
func TestProdFormatterCaller(t *testing.T) {
a := gtest.New(t)
l := logrus.New()
l.Out = ioutil.Discard
prodFormatter := NewProdFormatter()
l.Formatter = prodFormatter
buf := &bytes.Buffer{}
l.Hooks.Add(NewWriterHook(buf))
modifiers := []func(*logrus.Logger) *logrus.Entry{
func(logger *logrus.Logger) *logrus.Entry {
return logrus.NewEntry(logger)
},
func(logger *logrus.Logger) *logrus.Entry {
return logger.WithFields(logrus.Fields{
"event": "testevent",
"topic": "testtopic",
"key": "testkey",
})
},
func(logger *logrus.Logger) *logrus.Entry {
return logger.WithFields(logrus.Fields{
"event": "testevent",
"topic": "testtopic",
"key": "testkey",
}).WithFields(logrus.Fields{
"event1": "testevent",
"topic1": "testtopic",
"key1": "testkey",
}).WithFields(logrus.Fields{
"event2": "testevent",
"topic2": "testtopic",
"key2": "testkey",
})
},
func(logger *logrus.Logger) *logrus.Entry {
return logger.WithError(errors.New("some err"))
},
}
for i, mod := range modifiers {
buf.Reset()
modl := mod(l)
modl.Info("Some event")
// split := bytes.SplitN(buf.Bytes(), []byte(" "), 2)
// a.So(len(split), should.Equal, 2).Else(func(msg string) {
// t.Fatalf("\nfailed at index %d\n%s", i, msg)
// })
var v map[string]interface{}
err := json.Unmarshal(buf.Bytes(), &v)
a.So(err, should.BeNil).Else(func(msg string) {
t.Fatalf("\nfailed at index %d\n%s", i, msg)
})
// extra := v[prodFormatter.extraKey].(map[string]interface{})
// a.So(extra["file"], should.Equal, "formatter_test.go").Else(func(msg string) {
// t.Fatalf("\nfailed at index %d\n%s", i, msg)
// })
// a.So(extra["func"], should.Equal, "github.com/kdar/logrus-cloudwatchlogs.TestProdFormatterCaller").Else(func(msg string) {
// t.Fatalf("\nfailed at index %d\n%s", i, msg)
// })
}
}
func TestProdFormatterHTTPRequest(t *testing.T) {
a := gtest.New(t)
req, err := http.NewRequest("GET", "http://rxmanagement.net", nil)
a.So(err, should.BeNil).ElseFatal()
buf := &bytes.Buffer{}
l := logrus.New()
l.Out = buf
prodFormatter := NewProdFormatter(HTTPRequest("request"))
l.Formatter = prodFormatter
l.WithField("request", req).Print("with http request")
var v struct {
Request struct {
Host string
Method string
}
}
err = json.Unmarshal(buf.Bytes(), &v)
a.So(err, should.BeNil).ElseFatal()
a.So(v.Request.Host, should.Equal, "rxmanagement.net").ElseFatal()
a.So(v.Request.Method, should.Equal, "GET").ElseFatal()
}