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

Function NewLogger

zaptest/logger.go:77–101  ·  view source on GitHub ↗

NewLogger builds a new Logger that logs all messages to the given testing.TB. logger := zaptest.NewLogger(t) Use this with a *testing.T or *testing.B to get logs which get printed only if a test fails or if you ran go test -v. The returned logger defaults to logging debug level messages and abov

(t TestingT, opts ...LoggerOption)

Source from the content-addressed store, hash-verified

75//
76// logger := zaptest.NewLogger(t, zaptest.WrapOptions(zap.AddCaller()))
77func NewLogger(t TestingT, opts ...LoggerOption) *zap.Logger {
78 cfg := loggerOptions{
79 Level: zapcore.DebugLevel,
80 }
81 for _, o := range opts {
82 o.applyLoggerOption(&cfg)
83 }
84
85 writer := NewTestingWriter(t)
86 zapOptions := []zap.Option{
87 // Send zap errors to the same writer and mark the test as failed if
88 // that happens.
89 zap.ErrorOutput(writer.WithMarkFailed(true)),
90 }
91 zapOptions = append(zapOptions, cfg.zapOptions...)
92
93 return zap.New(
94 zapcore.NewCore(
95 zapcore.NewConsoleEncoder(zap.NewDevelopmentEncoderConfig()),
96 writer,
97 cfg.Level,
98 ),
99 zapOptions...,
100 )
101}
102
103// TestingWriter is a WriteSyncer that writes to the given testing.TB.
104type TestingWriter struct {

Callers 6

TestSlogtestFunction · 0.92
TestTestLoggerFunction · 0.70

Calls 8

WithMarkFailedMethod · 0.95
ErrorOutputFunction · 0.92
NewFunction · 0.92
NewCoreFunction · 0.92
NewConsoleEncoderFunction · 0.92
NewTestingWriterFunction · 0.85
applyLoggerOptionMethod · 0.65

Tested by 6

TestSlogtestFunction · 0.74
TestTestLoggerFunction · 0.56