Skip to content

Commit 27b96e3

Browse files
authored
Make zaptest.NewTestingWriter public (#1399)
**Add more flexibility in configuring zap logger for tests.** The default `zapcore.Core`, which is created in `zaptest.NewLogger()` may not be suitable for all use-cases. ``` func NewLogger(t TestingT, opts ...LoggerOption) *zap.Logger { ... return zap.New( zapcore.NewCore( zapcore.NewConsoleEncoder(zap.NewDevelopmentEncoderConfig()), writer, cfg.Level, ), zapOptions..., ) ``` E.g., we may need custom encoder or encoder config. This PR allows us to do such customization: ``` writer := zaptest.NewTestingWriter(t) core := zapcore.NewCore(encoder, writer, level) logger := zap.New(core, zap.AddCaller()) ```
1 parent 70f61bb commit 27b96e3

File tree

2 files changed

+28
-11
lines changed

2 files changed

+28
-11
lines changed

zaptest/logger.go

+27-10
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ func NewLogger(t TestingT, opts ...LoggerOption) *zap.Logger {
8282
o.applyLoggerOption(&cfg)
8383
}
8484

85-
writer := newTestingWriter(t)
85+
writer := NewTestingWriter(t)
8686
zapOptions := []zap.Option{
8787
// Send zap errors to the same writer and mark the test as failed if
8888
// that happens.
@@ -100,27 +100,43 @@ func NewLogger(t TestingT, opts ...LoggerOption) *zap.Logger {
100100
)
101101
}
102102

103-
// testingWriter is a WriteSyncer that writes to the given testing.TB.
104-
type testingWriter struct {
103+
// TestingWriter is a WriteSyncer that writes to the given testing.TB.
104+
type TestingWriter struct {
105105
t TestingT
106106

107-
// If true, the test will be marked as failed if this testingWriter is
107+
// If true, the test will be marked as failed if this TestingWriter is
108108
// ever used.
109109
markFailed bool
110110
}
111111

112-
func newTestingWriter(t TestingT) testingWriter {
113-
return testingWriter{t: t}
112+
// NewTestingWriter builds a new TestingWriter that writes to the given
113+
// testing.TB.
114+
//
115+
// Use this if you need more flexibility when creating *zap.Logger
116+
// than zaptest.NewLogger() provides.
117+
//
118+
// E.g., if you want to use custom core with zaptest.TestingWriter:
119+
//
120+
// encoder := newCustomEncoder()
121+
// writer := zaptest.NewTestingWriter(t)
122+
// level := zap.NewAtomicLevelAt(zapcore.DebugLevel)
123+
//
124+
// core := newCustomCore(encoder, writer, level)
125+
//
126+
// logger := zap.New(core, zap.AddCaller())
127+
func NewTestingWriter(t TestingT) TestingWriter {
128+
return TestingWriter{t: t}
114129
}
115130

116-
// WithMarkFailed returns a copy of this testingWriter with markFailed set to
131+
// WithMarkFailed returns a copy of this TestingWriter with markFailed set to
117132
// the provided value.
118-
func (w testingWriter) WithMarkFailed(v bool) testingWriter {
133+
func (w TestingWriter) WithMarkFailed(v bool) TestingWriter {
119134
w.markFailed = v
120135
return w
121136
}
122137

123-
func (w testingWriter) Write(p []byte) (n int, err error) {
138+
// Write writes bytes from p to the underlying testing.TB.
139+
func (w TestingWriter) Write(p []byte) (n int, err error) {
124140
n = len(p)
125141

126142
// Strip trailing newline because t.Log always adds one.
@@ -135,6 +151,7 @@ func (w testingWriter) Write(p []byte) (n int, err error) {
135151
return n, nil
136152
}
137153

138-
func (w testingWriter) Sync() error {
154+
// Sync commits the current contents (a no-op for TestingWriter).
155+
func (w TestingWriter) Sync() error {
139156
return nil
140157
}

zaptest/logger_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ func TestTestLoggerSupportsWrappedZapOptions(t *testing.T) {
106106

107107
func TestTestingWriter(t *testing.T) {
108108
ts := newTestLogSpy(t)
109-
w := newTestingWriter(ts)
109+
w := NewTestingWriter(ts)
110110

111111
n, err := io.WriteString(w, "hello\n\n")
112112
assert.NoError(t, err, "WriteString must not fail")

0 commit comments

Comments
 (0)