-
Notifications
You must be signed in to change notification settings - Fork 21
/
default_logger.go
144 lines (126 loc) · 3.38 KB
/
default_logger.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package log
import (
"fmt"
"io"
"os"
"strconv"
"sync"
)
const (
space = byte(' ')
equals = byte('=')
newLine = byte('\n')
base10 = 10
v = "%v"
)
// ConsoleBuilder is used to create a new console logger
type ConsoleBuilder struct {
writer io.Writer
timestampFormat string
}
// NewConsoleBuilder creates a new ConsoleBuilder for configuring and creating a new console logger
func NewConsoleBuilder() *ConsoleBuilder {
return &ConsoleBuilder{
writer: os.Stderr,
timestampFormat: DefaultTimeFormat,
}
}
func (b *ConsoleBuilder) WithWriter(writer io.Writer) *ConsoleBuilder {
b.writer = writer
return b
}
func (b *ConsoleBuilder) WithTimestampFormat(format string) *ConsoleBuilder {
b.timestampFormat = format
return b
}
func (b *ConsoleBuilder) Build() *Logger {
return &Logger{
writer: b.writer,
timestampFormat: b.timestampFormat,
}
}
// Logger is an instance of the console logger
type Logger struct {
m sync.Mutex
writer io.Writer
timestampFormat string
}
// Log handles the log entry
func (c *Logger) Log(e Entry) {
var lvl string
var i int
buff := BytePool().Get()
buff.B = append(buff.B, e.Timestamp.Format(c.timestampFormat)...)
buff.B = append(buff.B, space)
lvl = e.Level.String()
for i = 0; i < 6-len(lvl); i++ {
buff.B = append(buff.B, space)
}
buff.B = append(buff.B, lvl...)
buff.B = append(buff.B, space)
buff.B = append(buff.B, e.Message...)
c.addFields("", buff, e.Fields)
buff.B = append(buff.B, newLine)
c.m.Lock()
_, _ = c.writer.Write(buff.B)
c.m.Unlock()
BytePool().Put(buff)
}
func (c *Logger) addFields(prefix string, buff *Buffer, fields []Field) {
for _, f := range fields {
switch t := f.Value.(type) {
case string:
printKey(buff, prefix+f.Key)
buff.B = append(buff.B, t...)
case int:
printKey(buff, prefix+f.Key)
buff.B = strconv.AppendInt(buff.B, int64(t), base10)
case int8:
printKey(buff, prefix+f.Key)
buff.B = strconv.AppendInt(buff.B, int64(t), base10)
case int16:
printKey(buff, prefix+f.Key)
buff.B = strconv.AppendInt(buff.B, int64(t), base10)
case int32:
printKey(buff, prefix+f.Key)
buff.B = strconv.AppendInt(buff.B, int64(t), base10)
case int64:
printKey(buff, prefix+f.Key)
buff.B = strconv.AppendInt(buff.B, t, base10)
case uint:
printKey(buff, prefix+f.Key)
buff.B = strconv.AppendUint(buff.B, uint64(t), base10)
case uint8:
printKey(buff, prefix+f.Key)
buff.B = strconv.AppendUint(buff.B, uint64(t), base10)
case uint16:
printKey(buff, prefix+f.Key)
buff.B = strconv.AppendUint(buff.B, uint64(t), base10)
case uint32:
printKey(buff, prefix+f.Key)
buff.B = strconv.AppendUint(buff.B, uint64(t), base10)
case uint64:
printKey(buff, prefix+f.Key)
buff.B = strconv.AppendUint(buff.B, t, base10)
case float32:
printKey(buff, prefix+f.Key)
buff.B = strconv.AppendFloat(buff.B, float64(t), 'f', -1, 32)
case float64:
printKey(buff, prefix+f.Key)
buff.B = strconv.AppendFloat(buff.B, t, 'f', -1, 64)
case bool:
printKey(buff, prefix+f.Key)
buff.B = strconv.AppendBool(buff.B, t)
case []Field:
c.addFields(prefix+f.Key+".", buff, t)
default:
printKey(buff, prefix+f.Key)
buff.B = append(buff.B, fmt.Sprintf(v, f.Value)...)
}
}
}
func printKey(buff *Buffer, key string) {
buff.B = append(buff.B, space)
buff.B = append(buff.B, key...)
buff.B = append(buff.B, equals)
}