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

Function TestSlogtest

exp/zapslog/handler_test.go:383–419  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

381}
382
383func TestSlogtest(t *testing.T) {
384 var buff bytes.Buffer
385 core := zapcore.NewCore(
386 zapcore.NewJSONEncoder(zapcore.EncoderConfig{
387 TimeKey: slog.TimeKey,
388 MessageKey: slog.MessageKey,
389 LevelKey: slog.LevelKey,
390 EncodeLevel: zapcore.CapitalLevelEncoder,
391 EncodeTime: zapcore.RFC3339TimeEncoder,
392 }),
393 zapcore.AddSync(&buff),
394 zapcore.DebugLevel,
395 )
396
397 // zaptest doesn't expose the underlying core,
398 // so we'll extract it from the logger.
399 testCore := zaptest.NewLogger(t).Core()
400
401 handler := NewHandler(zapcore.NewTee(core, testCore))
402 err := slogtest.TestHandler(
403 handler,
404 func() []map[string]any {
405 // Parse the newline-delimted JSON in buff.
406 var entries []map[string]any
407
408 dec := json.NewDecoder(bytes.NewReader(buff.Bytes()))
409 for dec.More() {
410 var ent map[string]any
411 require.NoError(t, dec.Decode(&ent), "Error decoding log message")
412 entries = append(entries, ent)
413 }
414
415 return entries
416 },
417 )
418 require.NoError(t, err, "Unexpected error from slogtest.TestHandler")
419}

Callers

nothing calls this directly

Calls 8

BytesMethod · 0.95
NewCoreFunction · 0.92
NewJSONEncoderFunction · 0.92
AddSyncFunction · 0.92
NewLoggerFunction · 0.92
NewTeeFunction · 0.92
NewHandlerFunction · 0.85
CoreMethod · 0.80

Tested by

no test coverage detected