-
-
Notifications
You must be signed in to change notification settings - Fork 4k
/
Copy pathstd.go
51 lines (45 loc) · 922 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
47
48
49
50
51
package log
import (
"bytes"
"fmt"
"io"
"log"
"sync"
)
var _ Logger = (*stdLogger)(nil)
type stdLogger struct {
log *log.Logger
pool *sync.Pool
}
// NewStdLogger new a 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 print the kv pairs log.
func (l *stdLogger) Log(level Level, keyvals ...interface{}) error {
if len(keyvals) == 0 {
return nil
}
if (len(keyvals) & 1) == 1 {
keyvals = append(keyvals, "KEYVALS UNPAIRED")
}
buf := l.pool.Get().(*bytes.Buffer)
buf.WriteString(level.String())
for i := 0; i < len(keyvals); i += 2 {
_, _ = fmt.Fprintf(buf, " %s=%v", keyvals[i], keyvals[i+1])
}
_ = l.log.Output(4, buf.String()) //nolint:gomnd
buf.Reset()
l.pool.Put(buf)
return nil
}
func (l *stdLogger) Close() error {
return nil
}