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)
| 75 | // |
| 76 | // logger := zaptest.NewLogger(t, zaptest.WrapOptions(zap.AddCaller())) |
| 77 | func 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. |
| 104 | type TestingWriter struct { |