diff --git a/field_test.go b/field_test.go index ec4487c4b..589e4fc18 100644 --- a/field_test.go +++ b/field_test.go @@ -174,6 +174,20 @@ func TestMarshalerField(t *testing.T) { assertCanBeReused(t, Marshaler("foo", fakeUser{"phil"})) } +func TestIntsField(t *testing.T) { + assertFieldJSON(t, `"foo":[]`, Object("foo", []int{})) + assertFieldJSON(t, `"foo":[1]`, Object("foo", []int{1})) + assertFieldJSON(t, `"foo":[1,2,3]`, Object("foo", []int{1, 2, 3})) + assertCanBeReused(t, Object("foo", []int{1, 2, 3})) +} + +func TestStringsField(t *testing.T) { + assertFieldJSON(t, `"foo":[]`, Object("foo", []string{})) + assertFieldJSON(t, `"foo":["bar 1"]`, Object("foo", []string{"bar 1"})) + assertFieldJSON(t, `"foo":["bar 1","bar 2","bar 3"]`, Object("foo", []string{"bar 1", "bar 2", "bar 3"})) + assertCanBeReused(t, Object("foo", []string{"bar 1", "bar 2", "bar 3"})) +} + func TestObjectField(t *testing.T) { assertFieldJSON(t, `"foo":[5,6]`, Object("foo", []int{5, 6})) assertCanBeReused(t, Object("foo", []int{5, 6})) diff --git a/json_encoder_bench_test.go b/json_encoder_bench_test.go index a270d41bd..0311fae5e 100644 --- a/json_encoder_bench_test.go +++ b/json_encoder_bench_test.go @@ -45,6 +45,30 @@ func BenchmarkJSONLogMarshalerFunc(b *testing.B) { } } +func BenchmarkZapJSONInts(b *testing.B) { + ts := time.Unix(0, 0) + b.RunParallel(func(pb *testing.PB) { + for pb.Next() { + enc := NewJSONEncoder() + enc.AddObject("ints", []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}) + enc.WriteEntry(ioutil.Discard, "fake", DebugLevel, ts) + enc.Free() + } + }) +} + +func BenchmarkZapJSONStrings(b *testing.B) { + ts := time.Unix(0, 0) + b.RunParallel(func(pb *testing.PB) { + for pb.Next() { + enc := NewJSONEncoder() + enc.AddObject("strings", []string{"bar 1", "bar 2", "bar 3", "bar 4", "bar 5", "bar 6", "bar 7", "bar 8", "bar 9", "bar 10"}) + enc.WriteEntry(ioutil.Discard, "fake", DebugLevel, ts) + enc.Free() + } + }) +} + func BenchmarkZapJSON(b *testing.B) { ts := time.Unix(0, 0) b.RunParallel(func(pb *testing.PB) { diff --git a/json_encoder_test.go b/json_encoder_test.go index 9a59820f3..840b9ce88 100644 --- a/json_encoder_test.go +++ b/json_encoder_test.go @@ -140,6 +140,11 @@ func TestJSONEncoderFields(t *testing.T) { {"marshaler", `"k":{}`, func(e Encoder) { assert.Error(t, e.AddMarshaler("k", loggable{false}), "Expected an error calling MarshalLog.") }}, + {"ints", `"k":[1,2,3]`, func(e Encoder) { e.AddObject("k", []int{1, 2, 3}) }}, + {"strings", `"k":["bar 1","bar 2","bar 3"]`, + func(e Encoder) { + e.AddObject("k", []string{"bar 1", "bar 2", "bar 3"}) + }}, {"arbitrary object", `"k":{"loggable":"yes"}`, func(e Encoder) { assert.NoError(t, e.AddObject("k", map[string]string{"loggable": "yes"}), "Unexpected error JSON-serializing a map.") }}, diff --git a/logger_bench_test.go b/logger_bench_test.go index 5967f2726..d3e0c65f6 100644 --- a/logger_bench_test.go +++ b/logger_bench_test.go @@ -135,6 +135,18 @@ func BenchmarkMarshalerField(b *testing.B) { }) } +func BenchmarkIntsField(b *testing.B) { + withBenchedLogger(b, func(log zap.Logger) { + log.Info("Array of Ints.", zap.Object("ints", []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10})) + }) +} + +func BenchmarkStringsField(b *testing.B) { + withBenchedLogger(b, func(log zap.Logger) { + log.Info("Array of Strings.", zap.Object("strings", []string{"bar 1", "bar 2", "bar 3", "bar 4", "bar 5", "bar 6", "bar 7", "bar 8", "bar 9", "bar 10"})) + }) +} + func BenchmarkObjectField(b *testing.B) { withBenchedLogger(b, func(log zap.Logger) { log.Info("Reflection-based serialization.", zap.Object("user", _jane)) diff --git a/text_encoder_test.go b/text_encoder_test.go index 38674399a..e2c37ba8a 100644 --- a/text_encoder_test.go +++ b/text_encoder_test.go @@ -87,6 +87,11 @@ func TestTextEncoderFields(t *testing.T) { {"marshaler", "k={}", func(e Encoder) { assert.Error(t, e.AddMarshaler("k", loggable{false}), "Expected an error calling MarshalLog.") }}, + {"ints", "k=[1 2 3]", func(e Encoder) { e.AddObject("k", []int{1, 2, 3}) }}, + {"strings", `k=[bar 1 bar 2 bar 3]`, + func(e Encoder) { + e.AddObject("k", []string{"bar 1", "bar 2", "bar 3"}) + }}, {"map[string]string", "k=map[loggable:yes]", func(e Encoder) { assert.NoError(t, e.AddObject("k", map[string]string{"loggable": "yes"}), "Unexpected error serializing a map.") }},