@@ -27,7 +27,8 @@ import (
27
27
"time"
28
28
29
29
"go.uber.org/zap"
30
- "go.uber.org/zap/zwrap"
30
+ "go.uber.org/zap/testutils"
31
+ "go.uber.org/zap/zapcore"
31
32
)
32
33
33
34
var errExample = errors .New ("fail" )
@@ -38,10 +39,10 @@ type user struct {
38
39
CreatedAt time.Time `json:"created_at"`
39
40
}
40
41
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 ())
45
46
return nil
46
47
}
47
48
@@ -51,8 +52,27 @@ var _jane = user{
51
52
CreatedAt : time .Date (1980 , 1 , 1 , 12 , 0 , 0 , 0 , time .UTC ),
52
53
}
53
54
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 {
56
76
zap .Int ("int" , 1 ),
57
77
zap .Int64 ("int64" , 2 ),
58
78
zap .Float64 ("float" , 3.0 ),
@@ -61,7 +81,7 @@ func fakeFields() []zap.Field {
61
81
zap .Time ("time" , time .Unix (0 , 0 )),
62
82
zap .Error (errExample ),
63
83
zap .Duration ("duration" , time .Second ),
64
- zap .Marshaler ("user-defined type" , _jane ),
84
+ zap .Object ("user-defined type" , _jane ),
65
85
zap .String ("another string" , "done!" ),
66
86
}
67
87
}
@@ -75,9 +95,9 @@ func fakeMessages(n int) []string {
75
95
}
76
96
77
97
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 {} ,
81
101
zap .ErrorLevel ,
82
102
))
83
103
b .ResetTimer ()
@@ -91,9 +111,9 @@ func BenchmarkZapDisabledLevelsWithoutFields(b *testing.B) {
91
111
func BenchmarkZapDisabledLevelsAccumulatedContext (b * testing.B ) {
92
112
context := fakeFields ()
93
113
logger := zap .New (
94
- zap .WriterFacility (
95
- zap . NewJSONEncoder (),
96
- zap . Discard ,
114
+ zapcore .WriterFacility (
115
+ benchEncoder (),
116
+ & testutils. Discarder {} ,
97
117
zap .ErrorLevel ,
98
118
),
99
119
zap .Fields (context ... ),
@@ -107,9 +127,9 @@ func BenchmarkZapDisabledLevelsAccumulatedContext(b *testing.B) {
107
127
}
108
128
109
129
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 {} ,
113
133
zap .ErrorLevel ,
114
134
))
115
135
b .ResetTimer ()
@@ -121,9 +141,9 @@ func BenchmarkZapDisabledLevelsAddingFields(b *testing.B) {
121
141
}
122
142
123
143
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 {} ,
127
147
zap .ErrorLevel ,
128
148
))
129
149
b .ResetTimer ()
@@ -137,9 +157,9 @@ func BenchmarkZapDisabledLevelsCheckAddingFields(b *testing.B) {
137
157
}
138
158
139
159
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 {} ,
143
163
zap .DebugLevel ,
144
164
))
145
165
b .ResetTimer ()
@@ -153,9 +173,9 @@ func BenchmarkZapAddingFields(b *testing.B) {
153
173
func BenchmarkZapWithAccumulatedContext (b * testing.B ) {
154
174
context := fakeFields ()
155
175
logger := zap .New (
156
- zap .WriterFacility (
157
- zap . NewJSONEncoder (),
158
- zap . Discard ,
176
+ zapcore .WriterFacility (
177
+ benchEncoder (),
178
+ & testutils. Discarder {} ,
159
179
zap .DebugLevel ,
160
180
),
161
181
zap .Fields (context ... ),
@@ -169,9 +189,9 @@ func BenchmarkZapWithAccumulatedContext(b *testing.B) {
169
189
}
170
190
171
191
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 {} ,
175
195
zap .DebugLevel ,
176
196
))
177
197
b .ResetTimer ()
@@ -184,9 +204,9 @@ func BenchmarkZapWithoutFields(b *testing.B) {
184
204
185
205
func BenchmarkZapSampleWithoutFields (b * testing.B ) {
186
206
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 {} ,
190
210
zap .DebugLevel ,
191
211
), time .Second , 10 , 10000 ))
192
212
b .ResetTimer ()
@@ -201,9 +221,9 @@ func BenchmarkZapSampleWithoutFields(b *testing.B) {
201
221
202
222
func BenchmarkZapSampleAddingFields (b * testing.B ) {
203
223
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 {} ,
207
227
zap .DebugLevel ,
208
228
), time .Second , 10 , 10000 ))
209
229
b .ResetTimer ()
@@ -218,9 +238,9 @@ func BenchmarkZapSampleAddingFields(b *testing.B) {
218
238
219
239
func BenchmarkZapSampleCheckWithoutFields (b * testing.B ) {
220
240
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 {} ,
224
244
zap .DebugLevel ,
225
245
), time .Second , 10 , 10000 ))
226
246
b .ResetTimer ()
@@ -237,9 +257,9 @@ func BenchmarkZapSampleCheckWithoutFields(b *testing.B) {
237
257
238
258
func BenchmarkZapSampleCheckAddingFields (b * testing.B ) {
239
259
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 {} ,
243
263
zap .DebugLevel ,
244
264
), time .Second , 10 , 10000 ))
245
265
b .ResetTimer ()
0 commit comments