Skip to content

Commit 5c9ffae

Browse files
author
Akshay Shah
committed
Migrate zap to use zapcore
Migrate zap to use the new zapcore package, dropping all references to the `spy` and `spywrite` packages along the way. This collects most types and functions relevant to zap's end users in the base package.
1 parent 6bd8382 commit 5c9ffae

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+707
-4296
lines changed

benchmarks/logrus_bench_test.go

-55
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,7 @@ import (
2525
"testing"
2626
"time"
2727

28-
"go.uber.org/zap"
29-
"go.uber.org/zap/zbark"
30-
3128
"github.com/Sirupsen/logrus"
32-
"github.com/uber-common/bark"
3329
)
3430

3531
func newLogrus() *logrus.Logger {
@@ -62,31 +58,6 @@ func BenchmarkLogrusAddingFields(b *testing.B) {
6258
})
6359
}
6460

65-
func BenchmarkZapBarkifyAddingFields(b *testing.B) {
66-
logger := zbark.Barkify(zap.New(zap.WriterFacility(
67-
zap.NewJSONEncoder(),
68-
zap.Discard,
69-
zap.DebugLevel,
70-
)))
71-
b.ResetTimer()
72-
b.RunParallel(func(pb *testing.PB) {
73-
for pb.Next() {
74-
logger.WithFields(bark.Fields{
75-
"int": 1,
76-
"int64": int64(1),
77-
"float": 3.0,
78-
"string": "four!",
79-
"bool": true,
80-
"time": time.Unix(0, 0),
81-
"error": errExample.Error(),
82-
"duration": time.Second,
83-
"user-defined type": _jane,
84-
"another string": "done!",
85-
}).Info("Go fast.")
86-
}
87-
})
88-
}
89-
9061
func BenchmarkLogrusWithAccumulatedContext(b *testing.B) {
9162
baseLogger := newLogrus()
9263
logger := baseLogger.WithFields(logrus.Fields{
@@ -109,32 +80,6 @@ func BenchmarkLogrusWithAccumulatedContext(b *testing.B) {
10980
})
11081
}
11182

112-
func BenchmarkZapBarkifyWithAccumulatedContext(b *testing.B) {
113-
baseLogger := zbark.Barkify(zap.New(zap.WriterFacility(
114-
zap.NewJSONEncoder(),
115-
zap.Discard,
116-
zap.DebugLevel,
117-
)))
118-
logger := baseLogger.WithFields(bark.Fields{
119-
"int": 1,
120-
"int64": int64(1),
121-
"float": 3.0,
122-
"string": "four!",
123-
"bool": true,
124-
"time": time.Unix(0, 0),
125-
"error": errExample.Error(),
126-
"duration": time.Second,
127-
"user-defined type": _jane,
128-
"another string": "done!",
129-
})
130-
b.ResetTimer()
131-
b.RunParallel(func(pb *testing.PB) {
132-
for pb.Next() {
133-
logger.Info("Go really fast.")
134-
}
135-
})
136-
}
137-
13883
func BenchmarkLogrusWithoutFields(b *testing.B) {
13984
logger := newLogrus()
14085
b.ResetTimer()

benchmarks/stdlib_bench_test.go

+16-16
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,7 @@ import (
2424
"io/ioutil"
2525
"log"
2626
"testing"
27-
28-
"go.uber.org/zap"
29-
"go.uber.org/zap/zwrap"
27+
"time"
3028
)
3129

3230
func BenchmarkStandardLibraryWithoutFields(b *testing.B) {
@@ -39,22 +37,24 @@ func BenchmarkStandardLibraryWithoutFields(b *testing.B) {
3937
})
4038
}
4139

42-
func BenchmarkZapStandardizeWithoutFields(b *testing.B) {
43-
logger, err := zwrap.Standardize(
44-
zap.New(zap.WriterFacility(
45-
zap.NewJSONEncoder(),
46-
zap.Discard,
47-
zap.DebugLevel,
48-
)),
49-
zap.InfoLevel,
50-
)
51-
if err != nil {
52-
panic("Failed to Standardize a zap.Logger.")
53-
}
40+
func BenchmarkStandardLibraryWithFormatting(b *testing.B) {
41+
logger := log.New(ioutil.Discard, "", log.LstdFlags)
5442
b.ResetTimer()
5543
b.RunParallel(func(pb *testing.PB) {
5644
for pb.Next() {
57-
logger.Println("Go fast.")
45+
logger.Printf(
46+
"Go fast. %v %v %v %s %v %v %v %v %v %s\n",
47+
1,
48+
int64(1),
49+
3.0,
50+
"four!",
51+
true,
52+
time.Unix(0, 0),
53+
errExample,
54+
time.Second,
55+
_jane,
56+
"done!",
57+
)
5858
}
5959
})
6060
}

benchmarks/zap_bench_test.go

+61-41
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ import (
2727
"time"
2828

2929
"go.uber.org/zap"
30-
"go.uber.org/zap/zwrap"
30+
"go.uber.org/zap/testutils"
31+
"go.uber.org/zap/zapcore"
3132
)
3233

3334
var errExample = errors.New("fail")
@@ -38,10 +39,10 @@ type user struct {
3839
CreatedAt time.Time `json:"created_at"`
3940
}
4041

41-
func (u user) MarshalLog(kv zap.KeyValue) error {
42-
kv.AddString("name", u.Name)
43-
kv.AddString("email", u.Email)
44-
kv.AddInt64("created_at", u.CreatedAt.UnixNano())
42+
func (u user) MarshalLogObject(enc zapcore.ObjectEncoder) error {
43+
enc.AddString("name", u.Name)
44+
enc.AddString("email", u.Email)
45+
enc.AddInt64("created_at", u.CreatedAt.UnixNano())
4546
return nil
4647
}
4748

@@ -51,8 +52,27 @@ var _jane = user{
5152
CreatedAt: time.Date(1980, 1, 1, 12, 0, 0, 0, time.UTC),
5253
}
5354

54-
func fakeFields() []zap.Field {
55-
return []zap.Field{
55+
// TODO: remove this when we figure out a new config & options story.
56+
func benchEncoder() zapcore.Encoder {
57+
msgF := func(msg string) zapcore.Field {
58+
return zapcore.Field{Type: zapcore.StringType, String: msg, Key: "msg"}
59+
}
60+
timeF := func(t time.Time) zapcore.Field {
61+
millis := t.UnixNano() / int64(time.Millisecond)
62+
return zapcore.Field{Type: zapcore.Int64Type, Integer: millis, Key: "ts"}
63+
}
64+
levelF := func(l zapcore.Level) zapcore.Field {
65+
return zapcore.Field{Type: zapcore.StringType, String: l.String(), Key: "level"}
66+
}
67+
return zapcore.NewJSONEncoder(zapcore.JSONConfig{
68+
MessageFormatter: msgF,
69+
TimeFormatter: timeF,
70+
LevelFormatter: levelF,
71+
})
72+
}
73+
74+
func fakeFields() []zapcore.Field {
75+
return []zapcore.Field{
5676
zap.Int("int", 1),
5777
zap.Int64("int64", 2),
5878
zap.Float64("float", 3.0),
@@ -61,7 +81,7 @@ func fakeFields() []zap.Field {
6181
zap.Time("time", time.Unix(0, 0)),
6282
zap.Error(errExample),
6383
zap.Duration("duration", time.Second),
64-
zap.Marshaler("user-defined type", _jane),
84+
zap.Object("user-defined type", _jane),
6585
zap.String("another string", "done!"),
6686
}
6787
}
@@ -75,9 +95,9 @@ func fakeMessages(n int) []string {
7595
}
7696

7797
func BenchmarkZapDisabledLevelsWithoutFields(b *testing.B) {
78-
logger := zap.New(zap.WriterFacility(
79-
zap.NewJSONEncoder(),
80-
zap.Discard,
98+
logger := zap.New(zapcore.WriterFacility(
99+
benchEncoder(),
100+
&testutils.Discarder{},
81101
zap.ErrorLevel,
82102
))
83103
b.ResetTimer()
@@ -91,9 +111,9 @@ func BenchmarkZapDisabledLevelsWithoutFields(b *testing.B) {
91111
func BenchmarkZapDisabledLevelsAccumulatedContext(b *testing.B) {
92112
context := fakeFields()
93113
logger := zap.New(
94-
zap.WriterFacility(
95-
zap.NewJSONEncoder(),
96-
zap.Discard,
114+
zapcore.WriterFacility(
115+
benchEncoder(),
116+
&testutils.Discarder{},
97117
zap.ErrorLevel,
98118
),
99119
zap.Fields(context...),
@@ -107,9 +127,9 @@ func BenchmarkZapDisabledLevelsAccumulatedContext(b *testing.B) {
107127
}
108128

109129
func BenchmarkZapDisabledLevelsAddingFields(b *testing.B) {
110-
logger := zap.New(zap.WriterFacility(
111-
zap.NewJSONEncoder(),
112-
zap.Discard,
130+
logger := zap.New(zapcore.WriterFacility(
131+
benchEncoder(),
132+
&testutils.Discarder{},
113133
zap.ErrorLevel,
114134
))
115135
b.ResetTimer()
@@ -121,9 +141,9 @@ func BenchmarkZapDisabledLevelsAddingFields(b *testing.B) {
121141
}
122142

123143
func BenchmarkZapDisabledLevelsCheckAddingFields(b *testing.B) {
124-
logger := zap.New(zap.WriterFacility(
125-
zap.NewJSONEncoder(),
126-
zap.Discard,
144+
logger := zap.New(zapcore.WriterFacility(
145+
benchEncoder(),
146+
&testutils.Discarder{},
127147
zap.ErrorLevel,
128148
))
129149
b.ResetTimer()
@@ -137,9 +157,9 @@ func BenchmarkZapDisabledLevelsCheckAddingFields(b *testing.B) {
137157
}
138158

139159
func BenchmarkZapAddingFields(b *testing.B) {
140-
logger := zap.New(zap.WriterFacility(
141-
zap.NewJSONEncoder(),
142-
zap.Discard,
160+
logger := zap.New(zapcore.WriterFacility(
161+
benchEncoder(),
162+
&testutils.Discarder{},
143163
zap.DebugLevel,
144164
))
145165
b.ResetTimer()
@@ -153,9 +173,9 @@ func BenchmarkZapAddingFields(b *testing.B) {
153173
func BenchmarkZapWithAccumulatedContext(b *testing.B) {
154174
context := fakeFields()
155175
logger := zap.New(
156-
zap.WriterFacility(
157-
zap.NewJSONEncoder(),
158-
zap.Discard,
176+
zapcore.WriterFacility(
177+
benchEncoder(),
178+
&testutils.Discarder{},
159179
zap.DebugLevel,
160180
),
161181
zap.Fields(context...),
@@ -169,9 +189,9 @@ func BenchmarkZapWithAccumulatedContext(b *testing.B) {
169189
}
170190

171191
func BenchmarkZapWithoutFields(b *testing.B) {
172-
logger := zap.New(zap.WriterFacility(
173-
zap.NewJSONEncoder(),
174-
zap.Discard,
192+
logger := zap.New(zapcore.WriterFacility(
193+
benchEncoder(),
194+
&testutils.Discarder{},
175195
zap.DebugLevel,
176196
))
177197
b.ResetTimer()
@@ -184,9 +204,9 @@ func BenchmarkZapWithoutFields(b *testing.B) {
184204

185205
func BenchmarkZapSampleWithoutFields(b *testing.B) {
186206
messages := fakeMessages(1000)
187-
logger := zap.New(zwrap.Sample(zap.WriterFacility(
188-
zap.NewJSONEncoder(),
189-
zap.Discard,
207+
logger := zap.New(zapcore.Sample(zapcore.WriterFacility(
208+
benchEncoder(),
209+
&testutils.Discarder{},
190210
zap.DebugLevel,
191211
), time.Second, 10, 10000))
192212
b.ResetTimer()
@@ -201,9 +221,9 @@ func BenchmarkZapSampleWithoutFields(b *testing.B) {
201221

202222
func BenchmarkZapSampleAddingFields(b *testing.B) {
203223
messages := fakeMessages(1000)
204-
logger := zap.New(zwrap.Sample(zap.WriterFacility(
205-
zap.NewJSONEncoder(),
206-
zap.Discard,
224+
logger := zap.New(zapcore.Sample(zapcore.WriterFacility(
225+
benchEncoder(),
226+
&testutils.Discarder{},
207227
zap.DebugLevel,
208228
), time.Second, 10, 10000))
209229
b.ResetTimer()
@@ -218,9 +238,9 @@ func BenchmarkZapSampleAddingFields(b *testing.B) {
218238

219239
func BenchmarkZapSampleCheckWithoutFields(b *testing.B) {
220240
messages := fakeMessages(1000)
221-
logger := zap.New(zwrap.Sample(zap.WriterFacility(
222-
zap.NewJSONEncoder(),
223-
zap.Discard,
241+
logger := zap.New(zapcore.Sample(zapcore.WriterFacility(
242+
benchEncoder(),
243+
&testutils.Discarder{},
224244
zap.DebugLevel,
225245
), time.Second, 10, 10000))
226246
b.ResetTimer()
@@ -237,9 +257,9 @@ func BenchmarkZapSampleCheckWithoutFields(b *testing.B) {
237257

238258
func BenchmarkZapSampleCheckAddingFields(b *testing.B) {
239259
messages := fakeMessages(1000)
240-
logger := zap.New(zwrap.Sample(zap.WriterFacility(
241-
zap.NewJSONEncoder(),
242-
zap.Discard,
260+
logger := zap.New(zapcore.Sample(zapcore.WriterFacility(
261+
benchEncoder(),
262+
&testutils.Discarder{},
243263
zap.DebugLevel,
244264
), time.Second, 10, 10000))
245265
b.ResetTimer()

common_test.go

+7-39
Original file line numberDiff line numberDiff line change
@@ -21,54 +21,22 @@
2121
package zap
2222

2323
import (
24-
"os"
2524
"sync"
2625
"testing"
2726

28-
"github.com/stretchr/testify/assert"
27+
"go.uber.org/zap/zapcore"
2928
)
3029

3130
func opts(opts ...Option) []Option {
3231
return opts
3332
}
3433

35-
type stubbedExit struct {
36-
Status *int
37-
}
38-
39-
func (se *stubbedExit) Unstub() {
40-
_exit = os.Exit
41-
}
42-
43-
func (se *stubbedExit) AssertNoExit(t testing.TB) {
44-
assert.Nil(t, se.Status, "Unexpected exit.")
45-
}
46-
47-
func (se *stubbedExit) AssertStatus(t testing.TB, expected int) {
48-
if assert.NotNil(t, se.Status, "Expected to exit.") {
49-
assert.Equal(t, expected, *se.Status, "Unexpected exit code.")
50-
}
51-
}
52-
53-
func stubExit() *stubbedExit {
54-
stub := &stubbedExit{}
55-
_exit = func(s int) { stub.Status = &s }
56-
return stub
57-
}
58-
59-
func withJSONLogger(t testing.TB, enab LevelEnabler, opts []Option, f func(Logger, *testBuffer)) {
60-
sink := &testBuffer{}
61-
errSink := &testBuffer{}
62-
63-
allOpts := make([]Option, 0, 2+len(opts))
64-
allOpts = append(allOpts, ErrorOutput(errSink))
65-
allOpts = append(allOpts, opts...)
66-
logger := New(
67-
WriterFacility(newJSONEncoder(NoTime()), sink, enab),
68-
allOpts...)
69-
70-
f(logger, sink)
71-
assert.Empty(t, errSink.String(), "Expected error sink to be empty.")
34+
// Here specifically to introduce an easily-identifiable filename for testing
35+
// stacktraces and caller skips.
36+
func withLogger(t testing.TB, e zapcore.LevelEnabler, opts []Option, f func(Logger, *zapcore.ObservedLogs)) {
37+
fac, logs := zapcore.NewObserver(e, 1024)
38+
log := New(fac, opts...)
39+
f(log, logs)
7240
}
7341

7442
func runConcurrently(goroutines, iterations int, wg *sync.WaitGroup, f func()) {

0 commit comments

Comments
 (0)