Package list golang-github-go-kit-kit / 5ffd9f0
Test that level values are correctly formatted in Logfmt and JSON. Chris Hines 4 years ago
1 changed file(s) with 38 addition(s) and 6 deletion(s). Raw diff Collapse all Expand all
22 import (
33 "bytes"
44 "errors"
5 "io"
56 "strings"
67 "testing"
78
103104 t.Errorf("want %v, have %v", want, have)
104105 }
105106 if want, have := ``, strings.TrimSpace(buf.String()); want != have {
106 t.Errorf("want %q, have %q", want, have)
107 t.Errorf("\nwant '%s'\nhave '%s'", want, have)
107108 }
108109 }
109110
119120 t.Errorf("want %v, have %v", want, have)
120121 }
121122 if want, have := `{"foo":"bar"}`, strings.TrimSpace(buf.String()); want != have {
122 t.Errorf("want %q, have %q", want, have)
123 t.Errorf("\nwant '%s'\nhave '%s'", want, have)
123124 }
124125 }
125126
134135 logger = log.NewContext(logger).With("caller", log.DefaultCaller)
135136
136137 level.Info(logger).Log("foo", "bar")
137 if want, have := `level=info caller=level_test.go:137 foo=bar`, strings.TrimSpace(buf.String()); want != have {
138 t.Errorf("want %q, have %q", want, have)
138 if want, have := `level=info caller=level_test.go:138 foo=bar`, strings.TrimSpace(buf.String()); want != have {
139 t.Errorf("\nwant '%s'\nhave '%s'", want, have)
139140 }
140141 }
141142
150151 logger = level.NewFilter(logger, level.AllowAll())
151152
152153 level.Info(logger).Log("foo", "bar")
153 if want, have := `caller=level_test.go:153 level=info foo=bar`, strings.TrimSpace(buf.String()); want != have {
154 t.Errorf("want %q, have %q", want, have)
154 if want, have := `caller=level_test.go:154 level=info foo=bar`, strings.TrimSpace(buf.String()); want != have {
155 t.Errorf("\nwant '%s'\nhave '%s'", want, have)
155156 }
156157 }
158
159 func TestLevelFormatting(t *testing.T) {
160 testCases := []struct {
161 name string
162 format func(io.Writer) log.Logger
163 output string
164 }{
165 {
166 name: "logfmt",
167 format: log.NewLogfmtLogger,
168 output: `level=info foo=bar`,
169 },
170 {
171 name: "JSON",
172 format: log.NewJSONLogger,
173 output: `{"foo":"bar","level":"info"}`,
174 },
175 }
176
177 for _, tc := range testCases {
178 t.Run(tc.name, func(t *testing.T) {
179 var buf bytes.Buffer
180
181 logger := tc.format(&buf)
182 level.Info(logger).Log("foo", "bar")
183 if want, have := tc.output, strings.TrimSpace(buf.String()); want != have {
184 t.Errorf("\nwant: '%s'\nhave '%s'", want, have)
185 }
186 })
187 }
188 }