Skip to content

Commit 3e3e6d8

Browse files
authored
build a test logger (#218)
* remove extra fmt calls in the tests * build a test logger * Make a logger only endpoint
1 parent 32e886f commit 3e3e6d8

File tree

4 files changed

+110
-5
lines changed

4 files changed

+110
-5
lines changed

router/router_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ func TestTracing(t *testing.T) {
9292
r.Post("/def/ghi", noop)
9393
r.Put("/asdf/", noop)
9494
r.Route("/sub", func(r Router) {
95-
r.Get("/path", noop) // func(w http.ResponseWriter, r *http.Request) error { fmt.Println("!!!!!"); return nil })
95+
r.Get("/path", noop)
9696
})
9797

9898
scenes := map[string]struct {

testutil/logger.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package testutil
2+
3+
import (
4+
"os"
5+
"testing"
6+
7+
"github.com/sirupsen/logrus"
8+
"github.com/sirupsen/logrus/hooks/test"
9+
)
10+
11+
// Opt is a function that will modify the logger used
12+
type Opt func(l *logrus.Logger)
13+
14+
// TL will build and return a test logger
15+
func TL(t *testing.T, opts ...Opt) *logrus.Entry {
16+
l, _ := TestLogger(t, opts...)
17+
return l
18+
}
19+
20+
// TestLogger will build a logger that is useful for debugging
21+
// it respects levels configured by the 'LOG_LEVEL' env var.
22+
// It takes opt functions to modify the logger used
23+
func TestLogger(t *testing.T, opts ...Opt) (*logrus.Entry, *test.Hook) {
24+
l := logrus.New()
25+
l.SetOutput(testLogWrapper{t})
26+
hook := test.NewLocal(l)
27+
l.SetReportCaller(true)
28+
if ll := os.Getenv("LOG_LEVEL"); ll != "" {
29+
level, err := logrus.ParseLevel(ll)
30+
if err != nil {
31+
t.Logf("Error parsing the log level env var (%s), defaulting to info", ll)
32+
level = logrus.InfoLevel
33+
}
34+
l.SetLevel(level)
35+
}
36+
37+
for _, o := range opts {
38+
o(l)
39+
}
40+
41+
return l.WithField("test", t.Name()), hook
42+
}
43+
44+
type testLogWrapper struct {
45+
t *testing.T
46+
}
47+
48+
func (w testLogWrapper) Write(p []byte) (n int, err error) {
49+
w.t.Log(string(p))
50+
return len(p), nil
51+
}
52+
53+
// OptSetLevel will override the env var used to configre the logger
54+
func OptSetLevel(lvl logrus.Level) Opt {
55+
return func(l *logrus.Logger) {
56+
l.SetLevel(lvl)
57+
}
58+
}
59+
60+
// OptReportCaller will override the reporting of the calling function info
61+
func OptReportCaller(b bool) Opt {
62+
return func(l *logrus.Logger) {
63+
l.SetReportCaller(b)
64+
}
65+
}

testutil/logger_test.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package testutil
2+
3+
import (
4+
"os"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
func TestLoggerWrapper(t *testing.T) {
11+
tl, h := TestLogger(t)
12+
tl.Info("this is a test")
13+
assert.Len(t, h.Entries, 1)
14+
assert.Equal(t, "this is a test", h.Entries[0].Message)
15+
assert.Equal(t, "TestLoggerWrapper", h.Entries[0].Data["test"])
16+
}
17+
18+
func TestLogWrapperLevel(t *testing.T) {
19+
testCases := []struct {
20+
desc string
21+
expected []string
22+
}{
23+
{desc: "DEBUG", expected: []string{"debug", "info", "warn", "error"}},
24+
{desc: "NONSENSE", expected: []string{"info", "warn", "error"}},
25+
{desc: "INFO", expected: []string{"info", "warn", "error"}},
26+
{desc: "ERROR", expected: []string{"error"}},
27+
}
28+
for _, tC := range testCases {
29+
t.Run(tC.desc, func(t *testing.T) {
30+
os.Setenv("LOG_LEVEL", tC.desc)
31+
tl, h := TestLogger(t)
32+
tl.Debug("debug")
33+
tl.Info("info")
34+
tl.Warn("warn")
35+
tl.Error("error")
36+
37+
logged := []string{}
38+
for _, entry := range h.Entries {
39+
logged = append(logged, entry.Message)
40+
}
41+
assert.Equal(t, tC.expected, logged)
42+
})
43+
}
44+
}

tracing/req_tracer_test.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package tracing
22

33
import (
4-
"fmt"
54
"net/http"
65
"net/http/httptest"
76
"strconv"
@@ -31,7 +30,6 @@ func TestTracerLogging(t *testing.T) {
3130
assert.Empty(t, e.Data["referrer"])
3231
assert.NotEmpty(t, e.Data["method"])
3332
assert.Equal(t, "http://whatever.com/something", e.Data["url"])
34-
fmt.Println(e.Data)
3533

3634
_ = SetLogField(r, "first", "second")
3735
SetFinalField(r, "final", "line").Info("should have the final here")
@@ -40,14 +38,12 @@ func TestTracerLogging(t *testing.T) {
4038
assert.NotEmpty(t, e.Data["request_id"])
4139
assert.Equal(t, "line", e.Data["final"])
4240
assert.Equal(t, "second", e.Data["first"])
43-
fmt.Println(e.Data)
4441

4542
rt.Info("Shouldn't have the final line")
4643
e = hook.LastEntry()
4744
assert.Equal(t, 2, len(e.Data))
4845
assert.NotEmpty(t, e.Data["request_id"])
4946
assert.Equal(t, "second", e.Data["first"])
50-
fmt.Println(e.Data)
5147

5248
rt.WriteHeader(http.StatusOK)
5349
rt.Write([]byte{0, 1, 2, 3})

0 commit comments

Comments
 (0)