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

Function TestSugarTemplatedLogging

sugar_test.go:361–396  ·  sugar_test.go::TestSugarTemplatedLogging
(t *testing.T)

Source from the content-addressed store, hash-verified

359}
360
361func TestSugarTemplatedLogging(t *testing.T) {
362 tests := []struct {
363 format string
364 args []interface{}
365 expect string
366 }{
367 {"", nil, ""},
368 {"foo", nil, "foo"},
369 // If the user fails to pass a template, degrade to fmt.Sprint.
370 {"", []interface{}{"foo"}, "foo"},
371 }
372
373 // Common to all test cases.
374 context := []interface{}{"foo", "bar"}
375 expectedFields := []Field{String("foo", "bar")}
376
377 for _, tt := range tests {
378 withSugar(t, DebugLevel, nil, func(logger *SugaredLogger, logs *observer.ObservedLogs) {
379 logger.With(context...).Debugf(tt.format, tt.args...)
380 logger.With(context...).Infof(tt.format, tt.args...)
381 logger.With(context...).Warnf(tt.format, tt.args...)
382 logger.With(context...).Errorf(tt.format, tt.args...)
383 logger.With(context...).DPanicf(tt.format, tt.args...)
384 logger.With(context...).Logf(ErrorLevel, tt.format, tt.args...)
385
386 expected := make([]observer.LoggedEntry, 6)
387 for i, lvl := range []zapcore.Level{DebugLevel, InfoLevel, WarnLevel, ErrorLevel, DPanicLevel, ErrorLevel} {
388 expected[i] = observer.LoggedEntry{
389 Entry: zapcore.Entry{Message: tt.expect, Level: lvl},
390 Context: expectedFields,
391 }
392 }
393 assert.Equal(t, expected, logs.AllUntimed(), "Unexpected log output.")
394 })
395 }
396}
397
398func TestSugarLnLogging(t *testing.T) {
399 tests := []struct {

Callers

nothing calls this directly

Calls 10

StringFunction · 0.85
withSugarFunction · 0.85
DebugfMethod · 0.80
WarnfMethod · 0.80
DPanicfMethod · 0.80
AllUntimedMethod · 0.80
WithMethod · 0.65
ErrorfMethod · 0.65
LogfMethod · 0.65
InfofMethod · 0.45

Tested by

no test coverage detected