-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
zaptest: Add testing.TB compatible logger #518
Changes from 1 commit
4f818a2
be2b5ea
a73cb3f
e78537e
9c4600e
316a110
ca6ac7d
660acc6
4de70e3
959d416
063f7a1
8dd84b6
3ebc5fc
cdd3f0b
2b3338c
d7b6a16
d0af2be
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
Zap's errorOutput is used exclusively to log internal errors. If this ever gets used in a test, something catastrophic happened and we should fail the test. This changes the logger returned by zaptest.NewLogger to implement this behavior by passing a `WriteSyncer` to `zap.ErrorOutput` that marks the test as failed upon being used. To test this, we set up a test logger and replace its core with one that will always fail to write.
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -65,17 +65,39 @@ func NewLogger(t TestingT, opts ...LoggerOption) *zap.Logger { | |
o.applyLoggerOption(&cfg) | ||
} | ||
|
||
writer := newTestingWriter(t) | ||
return zap.New( | ||
zapcore.NewCore( | ||
zapcore.NewConsoleEncoder(zap.NewDevelopmentEncoderConfig()), | ||
testingWriter{t}, | ||
writer, | ||
cfg.Level, | ||
), | ||
|
||
// Send zap errors to the same writer and mark the test as failed if | ||
// that happens. | ||
zap.ErrorOutput(writer.WithMarkFailed(true)), | ||
) | ||
} | ||
|
||
// testingWriter is a WriteSyncer that writes to the given testing.TB. | ||
type testingWriter struct{ t TestingT } | ||
type testingWriter struct { | ||
t TestingT | ||
|
||
// If true, the test will be marked as failed if this testingWriter is | ||
// ever used. | ||
markFailed bool | ||
} | ||
|
||
func newTestingWriter(t TestingT) testingWriter { | ||
return testingWriter{t: t} | ||
} | ||
|
||
// WithMarkFailed returns a copy of this testingWriter with markFailed set to | ||
// the provided value. | ||
func (w testingWriter) WithMarkFailed(v bool) testingWriter { | ||
w.markFailed = v | ||
return w | ||
} | ||
|
||
func (w testingWriter) Write(p []byte) (n int, err error) { | ||
n = len(p) | ||
|
@@ -85,6 +107,10 @@ func (w testingWriter) Write(p []byte) (n int, err error) { | |
|
||
// Note: t.Log is safe for concurrent use. | ||
w.t.Logf("%s", p) | ||
if w.markFailed { | ||
w.t.Fail() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we instead There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
} | ||
|
||
return n, nil | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
zap.New()
sets the error output to stderr which should be overridden here. Otherwise there is no way to override it becausetestingWriter
is not exported. And also I think it makes sense to do it.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes sense, although if you end up writing to
ErrorOutput
, we should probably fail the test as well since that implies a serious Zap-internal error has occurred. I'll create a separate follow-up diff for that.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Excellent point. It should fail the test.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in #566