1
0
mirror of https://github.com/go-kratos/kratos.git synced 2025-01-24 03:46:37 +02:00
kratos/log/std.go

46 lines
769 B
Go
Raw Normal View History

2021-02-17 17:14:47 +08:00
package log
import (
"bytes"
"fmt"
"io"
2021-02-17 17:14:47 +08:00
"log"
"sync"
)
var _ Logger = (*stdLogger)(nil)
type stdLogger struct {
log *log.Logger
pool *sync.Pool
}
// NewStdLogger new a std logger with options.
func NewStdLogger(w io.Writer) Logger {
2021-02-17 17:14:47 +08:00
return &stdLogger{
log: log.New(w, "", log.LstdFlags),
2021-02-17 17:14:47 +08:00
pool: &sync.Pool{
New: func() interface{} {
return new(bytes.Buffer)
},
},
}
}
// Print print the kv pairs log.
func (l *stdLogger) Print(pairs ...interface{}) {
if len(pairs) == 0 {
2021-02-17 17:14:47 +08:00
return
}
if len(pairs)%2 != 0 {
pairs = append(pairs, "")
2021-02-17 17:14:47 +08:00
}
buf := l.pool.Get().(*bytes.Buffer)
for i := 0; i < len(pairs); i += 2 {
fmt.Fprintf(buf, "%s=%v ", pairs[i], Value(pairs[i+1]))
2021-02-17 17:14:47 +08:00
}
l.log.Output(4, buf.String())
2021-02-17 17:14:47 +08:00
buf.Reset()
l.pool.Put(buf)
2021-02-17 17:14:47 +08:00
}