ConsoleWriter: reset buffer before returning it to the pool (#119)

The buffers put back into the pool should be equivalent to those generated by the `New` function.
This commit is contained in:
Ingmar Stein 2018-12-05 08:46:12 +01:00 committed by Olivier Poitrey
parent 8747b7b3a5
commit 7179aeef58
1 changed files with 10 additions and 5 deletions

View File

@ -37,7 +37,6 @@ var (
} }
consoleDefaultTimeFormat = time.Kitchen consoleDefaultTimeFormat = time.Kitchen
consoleDefaultFormatter = func(i interface{}) string { return fmt.Sprintf("%s", i) }
consoleDefaultPartsOrder = func() []string { consoleDefaultPartsOrder = func() []string {
return []string{ return []string{
TimestampFieldName, TimestampFieldName,
@ -113,7 +112,10 @@ func (w ConsoleWriter) Write(p []byte) (n int, err error) {
} }
var buf = consoleBufPool.Get().(*bytes.Buffer) var buf = consoleBufPool.Get().(*bytes.Buffer)
defer consoleBufPool.Put(buf) defer func() {
buf.Reset()
consoleBufPool.Put(buf)
}()
var evt map[string]interface{} var evt map[string]interface{}
p = decodeIfBinaryToBytes(p) p = decodeIfBinaryToBytes(p)
@ -130,9 +132,12 @@ func (w ConsoleWriter) Write(p []byte) (n int, err error) {
w.writeFields(evt, buf) w.writeFields(evt, buf)
buf.WriteByte('\n') err = buf.WriteByte('\n')
buf.WriteTo(w.Out) if err != nil {
return len(p), nil return n, err
}
_, err = buf.WriteTo(w.Out)
return len(p), err
} }
// writeFields appends formatted key-value pairs to buf. // writeFields appends formatted key-value pairs to buf.