-
Notifications
You must be signed in to change notification settings - Fork 0
/
std.go
46 lines (40 loc) · 797 Bytes
/
std.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package golog
import (
"bytes"
"fmt"
"io"
"log"
"sync"
)
type stdLogger struct {
log *log.Logger
pool *sync.Pool
}
// NewStdLogger new a standard logger with writer.
func NewStdLogger(w io.Writer) Logger {
return &stdLogger{
log: log.New(w, "", 0),
pool: &sync.Pool{
New: func() interface{} {
return new(bytes.Buffer)
},
},
}
}
// Log write the kv pairs log.
func (l *stdLogger) Log(level Level, kvs ...interface{}) {
if len(kvs) == 0 {
return
}
if (len(kvs) & 1) == 1 {
kvs = append(kvs, "KEY VALUES UNPAIRED")
}
buf := l.pool.Get().(*bytes.Buffer)
_, _ = buf.WriteString(level.String())
for i := 0; i < len(kvs); i += 2 {
_, _ = fmt.Fprintf(buf, `, "%v": "%v"`, kvs[i], kvs[i+1])
}
_ = l.log.Output(0, buf.String())
buf.Reset()
l.pool.Put(buf)
}