Skip to content
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

zapcore: Ignore nil Entry.Time in encoders #1369

Merged
merged 3 commits into from
Oct 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion zapcore/console_encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func (c consoleEncoder) EncodeEntry(ent Entry, fields []Field) (*buffer.Buffer,
// If this ever becomes a performance bottleneck, we can implement
// ArrayEncoder for our plain-text format.
arr := getSliceEncoder()
if c.TimeKey != "" && c.EncodeTime != nil {
if c.TimeKey != "" && c.EncodeTime != nil && !ent.Time.IsZero() {
c.EncodeTime(ent.Time, arr)
}
if c.LevelKey != "" && c.EncodeLevel != nil {
Expand Down
45 changes: 45 additions & 0 deletions zapcore/console_encoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package zapcore_test

import (
"testing"
"time"

"github.com/stretchr/testify/assert"
. "go.uber.org/zap/zapcore"
Expand All @@ -35,6 +36,50 @@ var testEntry = Entry{
Caller: EntryCaller{Defined: true, File: "foo.go", Line: 42, Function: "foo.Foo"},
}

func TestConsoleEncodeEntry(t *testing.T) {
tests := []struct {
desc string
expected string
ent Entry
fields []Field
}{
{
desc: "info no fields",
expected: "2018-06-19T16:33:42Z\tinfo\tbob\tlob law\n",
ent: Entry{
Level: InfoLevel,
Time: time.Date(2018, 6, 19, 16, 33, 42, 99, time.UTC),
LoggerName: "bob",
Message: "lob law",
},
},
{
desc: "zero_time_omitted",
expected: "info\tname\tmessage\n",
ent: Entry{
Level: InfoLevel,
Time: time.Time{},
LoggerName: "name",
Message: "message",
},
},
}

cfg := testEncoderConfig()
cfg.EncodeTime = RFC3339TimeEncoder
enc := NewConsoleEncoder(cfg)

for _, tt := range tests {
t.Run(tt.desc, func(t *testing.T) {
buf, err := enc.EncodeEntry(tt.ent, tt.fields)
if assert.NoError(t, err, "Unexpected console encoding error.") {
assert.Equal(t, tt.expected, buf.String(), "Incorrect encoded entry.")
}
buf.Free()
})
}
}

func TestConsoleSeparator(t *testing.T) {
tests := []struct {
desc string
Expand Down
2 changes: 1 addition & 1 deletion zapcore/json_encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ func (enc *jsonEncoder) EncodeEntry(ent Entry, fields []Field) (*buffer.Buffer,
final.AppendString(ent.Level.String())
}
}
if final.TimeKey != "" {
if final.TimeKey != "" && !ent.Time.IsZero() {
final.AddTime(final.TimeKey, ent.Time)
}
if ent.LoggerName != "" && final.NameKey != "" {
Expand Down
14 changes: 14 additions & 0 deletions zapcore/json_encoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,20 @@ func TestJSONEncodeEntry(t *testing.T) {
}),
},
},
{
desc: "zero_time_omitted",
expected: `{
Comment on lines +112 to +114
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice. Would you mind adding a similar check to the console_encoder_test.go as well?

"L": "info",
"N": "name",
"M": "message"
}`,
ent: zapcore.Entry{
Level: zapcore.InfoLevel,
Time: time.Time{},
LoggerName: "name",
Message: "message",
},
},
}

enc := zapcore.NewJSONEncoder(zapcore.EncoderConfig{
Expand Down