-
Notifications
You must be signed in to change notification settings - Fork 0
/
level.go
419 lines (366 loc) · 11.5 KB
/
level.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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
package slog
import (
"context"
"fmt"
logslog "log/slog"
"strings"
"github.com/hedzr/is/states"
"github.com/hedzr/is/term/color"
)
type Level int
// SaveLevelAndSet sets Default logger's level and default logger level.
//
// SaveLevelAndSet saves old level and return a functor to restore it. So
// a typical usage is:
//
// func A() {
// defer slog.SaveLevelAndSet(slog.PanicLevel)()
// l := slog.New().WithLevel(slog.WarnLevel)
// ...
// }
//
// GetLevel and SetLevel can access the default logger level.
func SaveLevelAndSet(lvl Level) func() {
save := GetLevel()
SetLevel(lvl)
return func() {
SetLevel(save)
}
}
// GetLevel returns the default logger level
func GetLevel() Level { return lvlCurrent }
// SetLevel sets the default logger level
func SetLevel(lvl Level) {
lvlCurrent = lvl
if l, ok := defaultLog.(interface{ SetLevel(lvl Level) }); ok {
l.SetLevel(lvl)
} else if l, ok := defaultLog.(interface{ WithLevel(lvl Level) Entry }); ok {
l.WithLevel(lvl)
}
}
// ResetLevel restore the default logger level to factory value (WarnLevel).
func ResetLevel() { SetLevel(WarnLevel) }
// RegisterLevel records the given Level value as stocked.
func RegisterLevel(levelValue Level, title string, opts ...RegOpt) error {
for _, v := range allLevels {
if int(v) == int(levelValue) {
// return errorsv2.New("the given level %q is duplicated with %q", shortTags[5], v)
return fmt.Errorf("the given level %q is duplicated with %q", shortTagMap[5][v], v)
}
}
if l, ok := stringToLevel[title]; ok {
// return errorsv2.New("the title %q has been used for %q", title, l)
return fmt.Errorf("the title %q has been used for %q", title, l)
}
var pack = regPack{clr: color.NoColor,
bg: color.NoColor,
treatAs: MaxLevel,
}
for _, opt := range opts {
opt(&pack)
}
allLevels = append(allLevels, levelValue)
levelToString[levelValue] = title
stringToLevel[title] = levelValue
for i := 0; i < MaxLengthShortTag; i++ {
if str := pack.shortTags[i]; str != "" {
shortTagMap[i][levelValue] = str
}
}
if pack.clr != color.NoColor {
if pack.bg != color.NoColor {
mLevelColors[levelValue] = []color.Color{pack.clr, pack.bg}
} else {
mLevelColors[levelValue] = []color.Color{pack.clr}
}
}
if pack.treatAs < MaxLevel {
mLevelIsEnabledAs[levelValue] = pack.treatAs
}
if pack.printOutToErrorDevice {
mLevelUseErrorDevice[levelValue] = true
}
return nil
}
type regPack struct {
shortTags [MaxLengthShortTag]string
clr, bg color.Color
treatAs Level
printOutToErrorDevice bool
}
type RegOpt func(pack *regPack) // used by RegisterLevel
func RegWithShortTags(shortTags [MaxLengthShortTag]string) RegOpt {
return func(pack *regPack) {
pack.shortTags = shortTags
}
}
func RegWithColor(clr color.Color, bgColor ...color.Color) RegOpt {
return func(pack *regPack) {
pack.clr = clr
for _, bg := range bgColor {
pack.bg = bg
}
}
}
func RegWithTreatedAsLevel(treatAs Level) RegOpt {
return func(pack *regPack) {
pack.treatAs = treatAs
}
}
func RegWithPrintToErrorDevice(b ...bool) RegOpt {
return func(pack *regPack) {
for _, v := range b {
pack.printOutToErrorDevice = v
}
}
}
//
//
//
func (level Level) Enabled(ctx context.Context, testingLevel Level) bool {
if level == OffLevel || testingLevel == OffLevel {
return false
}
if level == AlwaysLevel || testingLevel == AlwaysLevel {
return true
}
if states.Env().GetDebugMode() && testingLevel == DebugLevel {
return true
}
if l, ok := mLevelIsEnabledAs[testingLevel]; ok {
testingLevel = l
}
return level >= testingLevel
}
// Convert the Level to a string. eg. PanicLevel becomes "panic".
func (level Level) String() string {
if b, err := level.MarshalText(); err == nil {
return string(b)
}
return "unknown"
}
// ShortTag convert Level to a short tag string. eg. PanicLevel becomes "P".
func (level Level) ShortTag(length int) string {
if length <= 0 || length >= MaxLengthShortTag {
panic("invalid length. the valid range: 1-5")
}
if va, ok := shortTagMap[length]; ok {
if ix, ok := va[level]; ok {
return ix
}
}
if t := level.String(); len(t) > 0 {
switch l := len(t); {
case l == length:
return t
case l < length:
t = t + strings.Repeat(" ", length)
fallthrough
default:
return t[:length]
}
}
return strings.Repeat("?", length)
}
func (level *Level) UnmarshalJSON(text []byte) error {
return level.UnmarshalText(text)
}
func (level Level) MarshalJSON() ([]byte, error) {
b, err := level.MarshalText()
return []byte(fmt.Sprintf("%q", string(b))), err
}
// UnmarshalText implements encoding.TextUnmarshaler.
func (level *Level) UnmarshalText(text []byte) error {
l, err := ParseLevel(string(text))
if err != nil {
return err
}
*level = Level(l)
return nil
}
// MarshalText convert Level to string and []byte.
//
// Available level names are:
// - "disable"
// - "fatal"
// - "error"
// - "warn"
// - "info"
// - "debug"
// - ...
func (level Level) MarshalText() ([]byte, error) {
if str, ok := levelToString[level]; ok {
return []byte(str), nil
}
return nil, fmt.Errorf("not a valid error level %d", level)
}
// ParseLevel takes a string level and returns the Logrus log level constant.
func ParseLevel(lvl string) (Level, error) {
if l, ok := stringToLevel[strings.ToLower(lvl)]; ok {
return l, nil
}
defaultLog.Warn("unknown logging level string", "string-value", lvl)
var l Level
return l, fmt.Errorf("not a valid logging Level: %q", lvl)
}
// AllLevels is a constant exposing all logging levels
func AllLevels() []Level { return allLevels }
// SetLevelColors defines your fore-/background color for printing
// the leveled text to terminal.
func SetLevelColors(lvl Level, fg, bg color.Color) {
mLevelColors[lvl] = []color.Color{fg, bg}
}
// mLevelIsEnabledAs is a replacement table of two levels.
//
// The given level is treated as another one (generally
// this means a builtin level, such as InfoLevel, ...).
//
// The feature is used for interpreting a user-defined
// Level to a internal builtin level so that we can
// judge its priority.
//
// What's the priorities?
//
// The builtin Level(s) are an integer so the smaller
// level has higher priority. If logger holding WarnLevel
// and user is requesting by Info(),
// since WarnLevel (3) < InfoLevel (4),
// so the lower priority requesting (for this case, a InfoLevel)
// will be revoked. It prints nothing to output device.
//
// But on the contrary, A Warn() will prints out for a
// logger holding InfoLevel level.
var mLevelIsEnabledAs = map[Level]Level{
OKLevel: InfoLevel,
SuccessLevel: InfoLevel,
FailLevel: ErrorLevel,
}
// mLevelUseErrorDevice maps Level to boolean flag which decides
// if the contents of that level will be written to error device
// or not.
var mLevelUseErrorDevice = map[Level]bool{
PanicLevel: true,
FatalLevel: true,
ErrorLevel: true,
WarnLevel: true,
FailLevel: true,
}
var mLevelColors = map[Level][]color.Color{
PanicLevel: {hiRed, clrNone}, //
FatalLevel: {hiRed, clrNone}, //
ErrorLevel: {red, clrNone}, //
WarnLevel: {yellow, clrNone}, //
InfoLevel: {cyan, clrNone}, //
DebugLevel: {color.FgMagenta, clrNone}, //
TraceLevel: {yellow, color.BgDim}, //
OffLevel: {color.FgBlack, color.BgDim}, // never used.
AlwaysLevel: {lightGray, color.BgBlink}, // blink color relies on concrete terminal but it commonly takes no effect.
OKLevel: {color.FgLightCyan, color.BgBlink}, //
SuccessLevel: {color.FgGreen, color.BgBlink}, //
FailLevel: {color.FgRed, color.BgBoldOrBright}, //
}
var shortTagMap = map[int]map[Level]string{
0: {},
1: {PanicLevel: "P", FatalLevel: "F", ErrorLevel: "E", WarnLevel: "W", InfoLevel: "I", DebugLevel: "D", TraceLevel: "T", OffLevel: " ", AlwaysLevel: "A", OKLevel: "o", SuccessLevel: "s", FailLevel: "f"},
2: {PanicLevel: "PC", FatalLevel: "FL", ErrorLevel: "ER", WarnLevel: "WN", InfoLevel: "IF", DebugLevel: "DG", TraceLevel: "TC", OffLevel: " ", AlwaysLevel: "AA", OKLevel: "OK", SuccessLevel: "SU", FailLevel: "FA"},
3: {PanicLevel: "PNC", FatalLevel: "FTL", ErrorLevel: "ERR", WarnLevel: "WRN", InfoLevel: "INF", DebugLevel: "DBG", TraceLevel: "TRC", OffLevel: " ", AlwaysLevel: " A ", OKLevel: " OK", SuccessLevel: "SUC", FailLevel: "FAI"},
4: {PanicLevel: "PNIC", FatalLevel: "FTAL", ErrorLevel: "ERRO", WarnLevel: "WARN", InfoLevel: "INFO", DebugLevel: "DBUG", TraceLevel: "TRAC", OffLevel: " ", AlwaysLevel: " AA ", OKLevel: " OK ", SuccessLevel: "SUCC", FailLevel: "FAIL"},
5: {PanicLevel: "PANIC", FatalLevel: "FATAL", ErrorLevel: "ERROR", WarnLevel: "WARNI", InfoLevel: "INFOR", DebugLevel: "DEBUG", TraceLevel: "TRACE", OffLevel: " ", AlwaysLevel: " A ", OKLevel: " OK ", SuccessLevel: "SUCCS", FailLevel: " FAIL"},
}
const MaxLengthShortTag = 6
var stringToLevel = map[string]Level{
"fail": FailLevel,
"success": SuccessLevel,
"ok": OKLevel,
"always": AlwaysLevel,
"off": OffLevel,
"no": OffLevel,
"disabled": OffLevel,
"trace": TraceLevel,
"debug": DebugLevel,
"devel": DebugLevel,
"dev": DebugLevel,
"develop": DebugLevel,
"info": InfoLevel,
"warn": WarnLevel,
"warning": WarnLevel,
"error": ErrorLevel,
"fatal": FatalLevel,
"panic": PanicLevel,
}
var levelToString = map[Level]string{
FailLevel: "fail",
SuccessLevel: "success",
OKLevel: "ok",
AlwaysLevel: "always",
OffLevel: "off",
TraceLevel: "trace",
DebugLevel: "debug",
InfoLevel: "info",
WarnLevel: "warning",
ErrorLevel: "error",
FatalLevel: "fatal",
PanicLevel: "panic",
}
var mLevelToLogSlog = map[Level]logslog.Level{
PanicLevel: logslog.LevelError, //
FatalLevel: logslog.LevelError, //
ErrorLevel: logslog.LevelError, //
WarnLevel: logslog.LevelWarn, //
InfoLevel: logslog.LevelInfo, //
DebugLevel: logslog.LevelDebug, //
TraceLevel: logslog.LevelDebug, //
OffLevel: logslog.LevelInfo, //
AlwaysLevel: logslog.LevelInfo, //
OKLevel: logslog.LevelInfo, //
SuccessLevel: logslog.LevelInfo, //
FailLevel: logslog.LevelInfo, //
}
var mLogSlogLevelToLevel = map[logslog.Level]Level{
logslog.LevelDebug: DebugLevel, // Level = -4
logslog.LevelInfo: InfoLevel, // Level = 0
logslog.LevelWarn: WarnLevel, // Level = 4
logslog.LevelError: ErrorLevel, // Level = 8
}
var allLevels = []Level{
PanicLevel,
FatalLevel,
ErrorLevel,
WarnLevel,
InfoLevel,
DebugLevel,
TraceLevel,
OffLevel,
AlwaysLevel,
OKLevel,
SuccessLevel,
FailLevel,
}
const (
// PanicLevel level, highest level of severity. Logs and then calls panic with the
// message passed to Debug, Info, ...
PanicLevel Level = iota
// FatalLevel level. Logs and then calls `os.Exit(-9)`. It will exit even if the
// logging level is set to PanicLevel.
FatalLevel
// ErrorLevel level. Logs. Used for errors that should definitely be noted.
// Commonly used for hooks to send errors to an error tracking service.
ErrorLevel
// WarnLevel level. Non-critical entries that deserve eyes.
WarnLevel
// InfoLevel level. General operational entries about what's going on inside the
// application.
InfoLevel
// DebugLevel level. Usually only enabled when debugging. Very verbose logging.
DebugLevel
// TraceLevel level. Designates finer-grained informational events than the DebugLevel.
TraceLevel
// OffLevel level. The logger will be shutdown.
OffLevel
// AlwaysLevel level. Used for Print, Printf, Println, OK, Success and Fail (use ErrorLevel).
AlwaysLevel
OKLevel
SuccessLevel
FailLevel
MaxLevel
)