diff --git a/zapcore/console_encoder.go b/zapcore/console_encoder.go index 8ca0bfaf5..cc2b4e07b 100644 --- a/zapcore/console_encoder.go +++ b/zapcore/console_encoder.go @@ -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 { diff --git a/zapcore/console_encoder_test.go b/zapcore/console_encoder_test.go index 439b4b885..8b42b6bc0 100644 --- a/zapcore/console_encoder_test.go +++ b/zapcore/console_encoder_test.go @@ -21,6 +21,7 @@ package zapcore_test import ( "testing" + "time" "github.com/stretchr/testify/assert" . "go.uber.org/zap/zapcore" @@ -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 diff --git a/zapcore/json_encoder.go b/zapcore/json_encoder.go index c8ab86979..9685169b2 100644 --- a/zapcore/json_encoder.go +++ b/zapcore/json_encoder.go @@ -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 != "" { diff --git a/zapcore/json_encoder_test.go b/zapcore/json_encoder_test.go index 4c651cf77..b2150256e 100644 --- a/zapcore/json_encoder_test.go +++ b/zapcore/json_encoder_test.go @@ -109,6 +109,20 @@ func TestJSONEncodeEntry(t *testing.T) { }), }, }, + { + desc: "zero_time_omitted", + expected: `{ + "L": "info", + "N": "name", + "M": "message" + }`, + ent: zapcore.Entry{ + Level: zapcore.InfoLevel, + Time: time.Time{}, + LoggerName: "name", + Message: "message", + }, + }, } enc := zapcore.NewJSONEncoder(zapcore.EncoderConfig{