MCPcopy
hub / github.com/uber-go/zap / TestSugarStructuredLogging

Function TestSugarStructuredLogging

sugar_test.go:290–326  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

288}
289
290func TestSugarStructuredLogging(t *testing.T) {
291 tests := []struct {
292 msg string
293 expectMsg string
294 }{
295 {"foo", "foo"},
296 {"", ""},
297 }
298
299 // Common to all test cases.
300 var (
301 err = errors.New("qux")
302 context = []interface{}{"foo", "bar"}
303 extra = []interface{}{err, "baz", false}
304 expectedFields = []Field{String("foo", "bar"), Error(err), Bool("baz", false)}
305 )
306
307 for _, tt := range tests {
308 withSugar(t, DebugLevel, nil, func(logger *SugaredLogger, logs *observer.ObservedLogs) {
309 logger.With(context...).Debugw(tt.msg, extra...)
310 logger.With(context...).Infow(tt.msg, extra...)
311 logger.With(context...).Warnw(tt.msg, extra...)
312 logger.With(context...).Errorw(tt.msg, extra...)
313 logger.With(context...).DPanicw(tt.msg, extra...)
314 logger.With(context...).Logw(WarnLevel, tt.msg, extra...)
315
316 expected := make([]observer.LoggedEntry, 6)
317 for i, lvl := range []zapcore.Level{DebugLevel, InfoLevel, WarnLevel, ErrorLevel, DPanicLevel, WarnLevel} {
318 expected[i] = observer.LoggedEntry{
319 Entry: zapcore.Entry{Message: tt.expectMsg, Level: lvl},
320 Context: expectedFields,
321 }
322 }
323 assert.Equal(t, expected, logs.AllUntimed(), "Unexpected log output.")
324 })
325 }
326}
327
328func TestSugarConcatenatingLogging(t *testing.T) {
329 tests := []struct {

Callers

nothing calls this directly

Calls 12

StringFunction · 0.85
ErrorFunction · 0.85
BoolFunction · 0.85
withSugarFunction · 0.85
DebugwMethod · 0.80
InfowMethod · 0.80
WarnwMethod · 0.80
ErrorwMethod · 0.80
DPanicwMethod · 0.80
LogwMethod · 0.80
AllUntimedMethod · 0.80
WithMethod · 0.65

Tested by

no test coverage detected