8 "github.com/go-logfmt/logfmt"
11 type logfmtEncoder struct {
16 func (l *logfmtEncoder) Reset() {
21 var logfmtEncoderPool = sync.Pool{
22 New: func() interface{} {
24 enc.Encoder = logfmt.NewEncoder(&enc.buf)
29 type logfmtLogger struct {
33 // NewLogfmtLogger returns a logger that encodes keyvals to the Writer in
34 // logfmt format. Each log event produces no more than one call to w.Write.
35 // The passed Writer must be safe for concurrent use by multiple goroutines if
36 // the returned Logger will be used concurrently.
37 func NewLogfmtLogger(w io.Writer) Logger {
38 return &logfmtLogger{w}
41 func (l logfmtLogger) Log(keyvals ...interface{}) error {
42 enc := logfmtEncoderPool.Get().(*logfmtEncoder)
44 defer logfmtEncoderPool.Put(enc)
46 if err := enc.EncodeKeyvals(keyvals...); err != nil {
50 // Add newline to the end of the buffer
51 if err := enc.EndRecord(); err != nil {
55 // The Logger interface requires implementations to be safe for concurrent
56 // use by multiple goroutines. For this implementation that means making
57 // only one call to l.w.Write() for each call to Log.
58 if _, err := l.w.Write(enc.buf.Bytes()); err != nil {