1 package logrusx_test
2
3 import (
4 "bytes"
5 "net/http"
6 "net/url"
7 "strconv"
8 "strings"
9 "testing"
10
11 "github.com/pkg/errors"
12 "github.com/sirupsen/logrus"
13 "github.com/stretchr/testify/assert"
14
15 "github.com/ory/herodot"
16
17 . "github.com/ory/x/logrusx"
18 )
19
20 var fakeRequest = &http.Request{
21 Method: "GET",
22 URL: &url.URL{Path: "/foo/bar", RawQuery: "bar=foo"},
23 Proto: "HTTP/1.1",
24 ProtoMajor: 1,
25 ProtoMinor: 1,
26 Header: http.Header{
27 "User-Agent": {"Go-http-client/1.1"},
28 "Accept-Encoding": {"gzip"},
29 "X-Request-Id": {"id1234"},
30 "Accept": {"application/json"},
31 },
32 Body: nil,
33 Host: "127.0.0.1:63232",
34 RemoteAddr: "127.0.0.1:63233",
35 RequestURI: "/foo/bar?bar=foo",
36 }
37
38 func TestOptions(t *testing.T) {
39 logger := New("", "", ForceLevel(logrus.DebugLevel))
40 assert.EqualValues(t, logrus.DebugLevel, logger.Logger.Level)
41 }
42
43 func TestJSONFormatter(t *testing.T) {
44 t.Run("pretty=true", func(t *testing.T) {
45 l := New("logrusx-audit", "v0.0.0", ForceFormat("json_pretty"), ForceLevel(logrus.DebugLevel))
46 var b bytes.Buffer
47 l.Logrus().Out = &b
48
49 l.Info("foo bar")
50 assert.True(t, strings.Count(b.String(), "\n") > 1)
51 assert.Contains(t, b.String(), " ")
52 })
53
54 t.Run("pretty=false", func(t *testing.T) {
55 l := New("logrusx-audit", "v0.0.0", ForceFormat("json"), ForceLevel(logrus.DebugLevel))
56 var b bytes.Buffer
57 l.Logrus().Out = &b
58
59 l.Info("foo bar")
60 assert.EqualValues(t, 1, strings.Count(b.String(), "\n"))
61 assert.NotContains(t, b.String(), " ")
62 })
63 }
64
65 func TestGelfFormatter(t *testing.T) {
66 t.Run("gelf formatter", func(t *testing.T) {
67 l := New("logrusx-audit", "v0.0.0", ForceFormat("gelf"), ForceLevel(logrus.DebugLevel))
68 var b bytes.Buffer
69 l.Logrus().Out = &b
70
71 l.Info("foo bar")
72 assert.Contains(t, b.String(), "_pid")
73 assert.Contains(t, b.String(), "level")
74 assert.Contains(t, b.String(), "short_message")
75 })
76 }
77
78 func TestTextLogger(t *testing.T) {
79 audit := NewAudit("logrusx-audit", "v0.0.0", ForceFormat("text"), ForceLevel(logrus.TraceLevel))
80 tracer := New("logrusx-app", "v0.0.0", ForceFormat("text"), ForceLevel(logrus.TraceLevel))
81 debugger := New("logrusx-server", "v0.0.1", ForceFormat("text"), ForceLevel(logrus.DebugLevel))
82 for k, tc := range []struct {
83 l *Logger
84 expect []string
85 notExpect []string
86 call func(l *Logger)
87 }{
88 {
89 l: audit,
90 expect: []string{"logrus_test.go", "logrusx_test.TestTextLogger",
91 "audience=audit", "service_name=logrusx-audit", "service_version=v0.0.0",
92 "An error occurred.", "message:some error", "trace", "testing.tRunner"},
93 call: func(l *Logger) {
94 l.WithError(errors.New("some error")).Error("An error occurred.")
95 },
96 },
97 {
98 l: tracer,
99 expect: []string{"logrus_test.go", "logrusx_test.TestTextLogger",
100 "audience=application", "service_name=logrusx-app", "service_version=v0.0.0",
101 "An error occurred.", "message:some error", "trace", "testing.tRunner"},
102 call: func(l *Logger) {
103 l.WithError(errors.New("some error")).Error("An error occurred.")
104 },
105 },
106 {
107 l: tracer,
108 expect: []string{"logrus_test.go", "logrusx_test.TestTextLogger",
109 "audience=application", "service_name=logrusx-app", "service_version=v0.0.0",
110 "An error occurred.", "headers:map[", "accept:application/json", "accept-encoding:gzip",
111 "user-agent:Go-http-client/1.1", "x-request-id:id1234", "host:127.0.0.1:63232", "method:GET",
112 "query:Value is sensitive and has been redacted. To see the value set config key \"log.leak_sensitive_values = true\" or environment variable \"LOG_LEAK_SENSITIVE_VALUES=true\".",
113 "remote:127.0.0.1:63233", "scheme:http", "path:/foo/bar",
114 },
115 notExpect: []string{"testing.tRunner", "bar=foo"},
116 call: func(l *Logger) {
117 l.WithRequest(fakeRequest).Error("An error occurred.")
118 },
119 },
120 {
121 l: New("logrusx-server", "v0.0.1", ForceFormat("text"), LeakSensitive(), ForceLevel(logrus.DebugLevel)),
122 expect: []string{
123 "audience=application", "service_name=logrusx-server", "service_version=v0.0.1",
124 "An error occurred.",
125 "headers:map[", "accept:application/json", "accept-encoding:gzip",
126 "user-agent:Go-http-client/1.1", "x-request-id:id1234", "host:127.0.0.1:63232", "method:GET",
127 "query:bar=foo",
128 "remote:127.0.0.1:63233", "scheme:http", "path:/foo/bar",
129 },
130 notExpect: []string{"logrus_test.go", "logrusx_test.TestTextLogger", "testing.tRunner", "?bar=foo"},
131 call: func(l *Logger) {
132 l.WithRequest(fakeRequest).Error("An error occurred.")
133 },
134 },
135 {
136 l: tracer,
137 expect: []string{"logrus_test.go", "logrusx_test.TestTextLogger",
138 "audience=application", "service_name=logrusx-app", "service_version=v0.0.0",
139 "An error occurred.", "message:The requested resource could not be found", "reason:some reason",
140 "status:Not Found", "status_code:404", "debug:some debug", "trace", "testing.tRunner"},
141 call: func(l *Logger) {
142 l.WithError(errors.WithStack(herodot.ErrNotFound.WithReason("some reason").WithDebug("some debug"))).Error("An error occurred.")
143 },
144 },
145 {
146 l: debugger,
147 expect: []string{"audience=application", "service_name=logrusx-server", "service_version=v0.0.1",
148 "An error occurred.", "message:some error"},
149 notExpect: []string{"logrus_test.go", "logrusx_test.TestTextLogger", "trace", "testing.tRunner"},
150 call: func(l *Logger) {
151 l.WithError(errors.New("some error")).Error("An error occurred.")
152 },
153 },
154 {
155 l: debugger,
156 expect: []string{"audience=application", "service_name=logrusx-server", "service_version=v0.0.1", "baz!", "foo=bar"},
157 notExpect: []string{"logrus_test.go", "logrusx_test.TestTextLogger"},
158 call: func(l *Logger) {
159 l.WithField("foo", "bar").Info("baz!")
160 },
161 },
162 } {
163 t.Run("case="+strconv.Itoa(k), func(t *testing.T) {
164 var b bytes.Buffer
165 tc.l.Logrus().Out = &b
166
167 tc.call(tc.l)
168
169 t.Logf(b.String())
170 for _, expect := range tc.expect {
171 assert.Contains(t, b.String(), expect)
172 }
173 for _, expect := range tc.notExpect {
174 assert.NotContains(t, b.String(), expect)
175 }
176 })
177 }
178 }
179
View as plain text