forked from gofiber/fiber
-
Notifications
You must be signed in to change notification settings - Fork 1
/
default.go
221 lines (181 loc) · 5.34 KB
/
default.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
package log
import (
"context"
"fmt"
"io"
"log"
"os"
"github.com/gofiber/utils/v2"
"github.com/valyala/bytebufferpool"
)
var _ AllLogger = (*defaultLogger)(nil)
type defaultLogger struct {
stdlog *log.Logger
level Level
depth int
}
// privateLog logs a message at a given level log the default logger.
// when the level is fatal, it will exit the program.
func (l *defaultLogger) privateLog(lv Level, fmtArgs []any) {
if l.level > lv {
return
}
level := lv.toString()
buf := bytebufferpool.Get()
buf.WriteString(level)
buf.WriteString(fmt.Sprint(fmtArgs...))
_ = l.stdlog.Output(l.depth, buf.String()) //nolint:errcheck // It is fine to ignore the error
if lv == LevelPanic {
panic(buf.String())
}
buf.Reset()
bytebufferpool.Put(buf)
if lv == LevelFatal {
os.Exit(1) //nolint:revive // we want to exit the program when Fatal is called
}
}
// privateLog logs a message at a given level log the default logger.
// when the level is fatal, it will exit the program.
func (l *defaultLogger) privateLogf(lv Level, format string, fmtArgs []any) {
if l.level > lv {
return
}
level := lv.toString()
buf := bytebufferpool.Get()
buf.WriteString(level)
if len(fmtArgs) > 0 {
_, _ = fmt.Fprintf(buf, format, fmtArgs...) //nolint: errcheck // It is fine to ignore the error
} else {
_, _ = fmt.Fprint(buf, fmtArgs...) //nolint: errcheck // It is fine to ignore the error
}
_ = l.stdlog.Output(l.depth, buf.String()) //nolint:errcheck // It is fine to ignore the error
if lv == LevelPanic {
panic(buf.String())
}
buf.Reset()
bytebufferpool.Put(buf)
if lv == LevelFatal {
os.Exit(1) //nolint:revive // we want to exit the program when Fatal is called
}
}
// privateLogw logs a message at a given level log the default logger.
// when the level is fatal, it will exit the program.
func (l *defaultLogger) privateLogw(lv Level, format string, keysAndValues []any) {
if l.level > lv {
return
}
level := lv.toString()
buf := bytebufferpool.Get()
buf.WriteString(level)
// Write format privateLog buffer
if format != "" {
buf.WriteString(format)
}
// Write keys and values privateLog buffer
if len(keysAndValues) > 0 {
if (len(keysAndValues) & 1) == 1 {
keysAndValues = append(keysAndValues, "KEYVALS UNPAIRED")
}
for i := 0; i < len(keysAndValues); i += 2 {
if i > 0 || format != "" {
buf.WriteByte(' ')
}
buf.WriteString(keysAndValues[i].(string)) //nolint:forcetypeassert,errcheck // Keys must be strings
buf.WriteByte('=')
buf.WriteString(utils.ToString(keysAndValues[i+1]))
}
}
_ = l.stdlog.Output(l.depth, buf.String()) //nolint:errcheck // It is fine to ignore the error
if lv == LevelPanic {
panic(buf.String())
}
buf.Reset()
bytebufferpool.Put(buf)
if lv == LevelFatal {
os.Exit(1) //nolint:revive // we want to exit the program when Fatal is called
}
}
func (l *defaultLogger) Trace(v ...any) {
l.privateLog(LevelTrace, v)
}
func (l *defaultLogger) Debug(v ...any) {
l.privateLog(LevelDebug, v)
}
func (l *defaultLogger) Info(v ...any) {
l.privateLog(LevelInfo, v)
}
func (l *defaultLogger) Warn(v ...any) {
l.privateLog(LevelWarn, v)
}
func (l *defaultLogger) Error(v ...any) {
l.privateLog(LevelError, v)
}
func (l *defaultLogger) Fatal(v ...any) {
l.privateLog(LevelFatal, v)
}
func (l *defaultLogger) Panic(v ...any) {
l.privateLog(LevelPanic, v)
}
func (l *defaultLogger) Tracef(format string, v ...any) {
l.privateLogf(LevelTrace, format, v)
}
func (l *defaultLogger) Debugf(format string, v ...any) {
l.privateLogf(LevelDebug, format, v)
}
func (l *defaultLogger) Infof(format string, v ...any) {
l.privateLogf(LevelInfo, format, v)
}
func (l *defaultLogger) Warnf(format string, v ...any) {
l.privateLogf(LevelWarn, format, v)
}
func (l *defaultLogger) Errorf(format string, v ...any) {
l.privateLogf(LevelError, format, v)
}
func (l *defaultLogger) Fatalf(format string, v ...any) {
l.privateLogf(LevelFatal, format, v)
}
func (l *defaultLogger) Panicf(format string, v ...any) {
l.privateLogf(LevelPanic, format, v)
}
func (l *defaultLogger) Tracew(msg string, keysAndValues ...any) {
l.privateLogw(LevelTrace, msg, keysAndValues)
}
func (l *defaultLogger) Debugw(msg string, keysAndValues ...any) {
l.privateLogw(LevelDebug, msg, keysAndValues)
}
func (l *defaultLogger) Infow(msg string, keysAndValues ...any) {
l.privateLogw(LevelInfo, msg, keysAndValues)
}
func (l *defaultLogger) Warnw(msg string, keysAndValues ...any) {
l.privateLogw(LevelWarn, msg, keysAndValues)
}
func (l *defaultLogger) Errorw(msg string, keysAndValues ...any) {
l.privateLogw(LevelError, msg, keysAndValues)
}
func (l *defaultLogger) Fatalw(msg string, keysAndValues ...any) {
l.privateLogw(LevelFatal, msg, keysAndValues)
}
func (l *defaultLogger) Panicw(msg string, keysAndValues ...any) {
l.privateLogw(LevelPanic, msg, keysAndValues)
}
func (l *defaultLogger) WithContext(_ context.Context) CommonLogger {
return &defaultLogger{
stdlog: l.stdlog,
level: l.level,
depth: l.depth - 1,
}
}
func (l *defaultLogger) SetLevel(level Level) {
l.level = level
}
func (l *defaultLogger) SetOutput(writer io.Writer) {
l.stdlog.SetOutput(writer)
}
// Logger returns the logger instance. It can be used to adjust the logger configurations in case of need.
func (l *defaultLogger) Logger() any {
return l.stdlog
}
// DefaultLogger returns the default logger.
func DefaultLogger() AllLogger {
return logger
}