Skip to content
Open
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
4 changes: 3 additions & 1 deletion encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,9 @@ func (enc *logfmtEncoder) EncodeEntry(ent zapcore.Entry, fields []zapcore.Field)
final.AppendString(ent.Message)
}
if enc.buf.Len() > 0 {
final.buf.AppendByte(' ')
if final.buf.Len() > 0 {
final.buf.AppendByte(' ')
}
final.buf.Write(enc.buf.Bytes())
}
addFields(final, fields)
Expand Down
24 changes: 23 additions & 1 deletion encoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func assertOutput(t testing.TB, desc string, expected string, f func(zapcore.Enc
f(enc)
expectedPrefix := `foo=bar`
if expected != "" {
// If we expect output, it should be comma-separated from the previous
// If we expect output, it should be space-separated from the previous
// field.
expectedPrefix += " "
}
Expand Down Expand Up @@ -189,3 +189,25 @@ main.main()
assert.Equal(t, `k=v stacktrace="panic: an unexpected error occurred\n\ngoroutine 1 [running]:\nmain.main()\n\t\t/go/src/github.com/jsternberg/myawesomeproject/h2g2.go:4 +0x39\n"
`, buf.String())
}

func TestEncodeEntryWithAddedField(t *testing.T) {
enc := &logfmtEncoder{buf: bufferpool.Get(), EncoderConfig: &zapcore.EncoderConfig{
EncodeTime: zapcore.EpochTimeEncoder,
EncodeDuration: zapcore.SecondsDurationEncoder,
}}
enc.AddString("x", "y")

buf, err := enc.EncodeEntry(
zapcore.Entry{
Level: zapcore.DebugLevel,
Time: time.Time{},
LoggerName: "test",
Message: "message",
},
[]zapcore.Field{
zap.String("k", "v"),
},
)
assert.NoError(t, err)
assert.Equal(t, "x=y k=v\n", buf.String())
}