-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.go
329 lines (273 loc) · 6.32 KB
/
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
package art
import (
"context"
"encoding"
"encoding/json"
"fmt"
"io"
"log"
"os"
"strconv"
"strings"
"time"
)
var loggerKey = "logger"
func CtxWithLogger(ctx context.Context, v Logger) context.Context {
return context.WithValue(ctx, &loggerKey, v)
}
func CtxGetLogger(ctx context.Context) Logger {
logger, ok := ctx.Value(&loggerKey).(Logger)
if !ok {
return DefaultLogger()
}
return logger
}
// Level
type LogLevel uint8
const (
LogLevelDebug LogLevel = iota + 1
LogLevelInfo
LogLevelWarn
LogLevelError
LogLevelFatal
)
func LookupLogLevel(level LogLevel) string {
return level_to_name[level]
}
var level_to_name = map[LogLevel]string{
LogLevelDebug: "debug",
LogLevelInfo: "info",
LogLevelWarn: "warn",
LogLevelError: "error",
LogLevelFatal: "fatal",
}
// Logger
type Logger interface {
Debug(format string, a ...any)
Info(format string, a ...any)
Warn(format string, a ...any)
Error(format string, a ...any)
Fatal(format string, a ...any)
SetLogLevel(level LogLevel)
LogLevel() LogLevel
WithCallDepth(externalDepth uint) Logger
WithKeyValue(key string, v any) Logger
}
var (
defaultLogger Logger = NewWriterLogger(os.Stdout, true, LogLevelDebug)
)
func SetDefaultLogger(l Logger) {
defaultLogger = l
}
func DefaultLogger() Logger {
return defaultLogger
}
func NewLogger(printPath bool, level LogLevel) Logger {
return NewWriterLogger(os.Stdout, printPath, level)
}
func NewWriterLogger(w io.Writer, printPath bool, level LogLevel) Logger {
flag := log.Lmsgprefix | log.LstdFlags
if printPath {
flag |= log.Llongfile
}
internalDepth := 2
return stdLogger{
debug: log.New(w, "[Debug] ", flag),
info: log.New(w, "[ Info] ", flag),
warn: log.New(w, "[ Warn] ", flag),
err: log.New(w, "[Error] ", flag),
fatal: log.New(w, "[Fatal] ", flag),
internalCallDepth: internalDepth,
logLevel: &level,
content: newContent(),
}
}
type stdLogger struct {
debug *log.Logger
info *log.Logger
warn *log.Logger
err *log.Logger
fatal *log.Logger
internalCallDepth int
logLevel *LogLevel
content content
}
func (l stdLogger) Debug(format string, a ...any) {
if *l.logLevel > LogLevelDebug {
return
}
l.debug.Output(l.internalCallDepth, encode(l.content, format, a...))
}
func (l stdLogger) Info(format string, a ...any) {
if *l.logLevel > LogLevelInfo {
return
}
l.info.Output(l.internalCallDepth, encode(l.content, format, a...))
}
func (l stdLogger) Warn(format string, a ...any) {
if *l.logLevel > LogLevelWarn {
return
}
l.warn.Output(l.internalCallDepth, encode(l.content, format, a...))
}
func (l stdLogger) Error(format string, a ...any) {
if *l.logLevel > LogLevelError {
return
}
l.err.Output(l.internalCallDepth, encode(l.content, format, a...))
}
func (l stdLogger) Fatal(format string, a ...any) {
if *l.logLevel > LogLevelFatal {
return
}
l.fatal.Output(l.internalCallDepth, encode(l.content, format, a...))
os.Exit(1)
}
func (l stdLogger) SetLogLevel(level LogLevel) {
*l.logLevel = level
}
func (l stdLogger) LogLevel() LogLevel {
return *l.logLevel
}
func (l stdLogger) WithCallDepth(externalDepth uint) Logger {
l.internalCallDepth += int(externalDepth)
return l
}
func (l stdLogger) WithKeyValue(key string, v any) Logger {
if key == "" {
return l
}
l.content = l.content.deepCopyAndSet(key, v)
return l
}
//
func newContent() content {
return content{
keys: make([]string, 0, 4),
contents: make(map[string]string, 4),
}
}
type content struct {
keys []string
contents map[string]string
}
func (c content) deepCopy() content {
size := len(c.keys)
keys := make([]string, size, cap(c.keys))
copy(keys, c.keys)
contents := make(map[string]string, size)
for key, v := range c.contents {
contents[key] = v
}
return content{keys: keys, contents: contents}
}
func (c content) deepCopyAndSet(key string, v any) content {
if key == "" {
return c
}
_, ok := c.contents[key]
if ok {
return c
}
fresh := c.deepCopy()
// set
fresh.keys = append(fresh.keys, key)
fresh.contents[key] = AnyToString(v)
return fresh
}
func encode(c content, format string, other ...any) string {
builder := &strings.Builder{}
for _, key := range c.keys {
if c.contents[key] == "" {
continue
}
builder.WriteString(key)
builder.WriteString("=")
builder.WriteString(c.contents[key])
builder.WriteString(" ")
}
fmt.Fprintf(builder, format, other...)
return builder.String()
}
func AnyToString(v any) string {
switch val := v.(type) {
case string:
return val
case []byte:
return string(val)
case bool:
return strconv.FormatBool(val)
case int:
return strconv.Itoa(val)
case int8:
return strconv.FormatInt(int64(val), 10)
case int16:
return strconv.FormatInt(int64(val), 10)
case int32:
return strconv.FormatInt(int64(val), 10)
case int64:
return strconv.FormatInt(val, 10)
case uint:
return strconv.FormatUint(uint64(val), 10)
case uint8:
return strconv.FormatUint(uint64(val), 10)
case uint16:
return strconv.FormatUint(uint64(val), 10)
case uint32:
return strconv.FormatUint(uint64(val), 10)
case uint64:
return strconv.FormatUint(val, 10)
case float32:
return strconv.FormatFloat(float64(val), 'f', -1, 32)
case float64:
return strconv.FormatFloat(val, 'f', -1, 64)
case error:
return val.Error()
case time.Duration:
return val.String()
case encoding.TextMarshaler:
text, err := val.MarshalText()
if err != nil {
return fmt.Sprintf("%v", val)
}
return string(text)
default:
bData, err := json.Marshal(val)
if err != nil {
return fmt.Sprintf("%v", val)
}
return string(bData)
}
}
//
func SilentLogger() Logger {
return silentLogger{}
}
type silentLogger struct{}
func (silentLogger) Debug(format string, a ...any) {
return
}
func (silentLogger) Info(format string, a ...any) {
return
}
func (silentLogger) Warn(format string, a ...any) {
return
}
func (silentLogger) Error(format string, a ...any) {
return
}
func (silentLogger) Fatal(format string, a ...any) {
return
}
func (silentLogger) SetLogLevel(level LogLevel) {
return
}
func (l silentLogger) LogLevel() LogLevel {
return 0
}
func (l silentLogger) WithCallDepth(externalDepth uint) Logger {
return l
}
func (l silentLogger) WithKeyValue(key string, v any) Logger {
return l
}