This repository has been archived by the owner on Jan 9, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
levels.go
117 lines (88 loc) · 2.16 KB
/
levels.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
package logur
import (
"fmt"
"strings"
)
// Level represents the same levels as defined in Logger.
type Level uint32
// Levels as defined in Logger.
const (
// Even more fine-grained information than Debug events.
// Loggers not supporting this level should fall back to Debug.
Trace Level = iota
// A verbose series of information events.
// They are useful when debugging the system.
Debug
// General information about what's happening inside the system.
Info
// Non-critical events that should be looked at.
Warn
// Critical events that require immediate attention.
Error
)
// ParseLevel takes a string level and returns the defined log level constant.
// If the level is not defined, it returns false as the second parameter.
func ParseLevel(level string) (Level, bool) {
switch strings.ToLower(level) {
case "trace":
return Trace, true
case "debug":
return Debug, true
case "info":
return Info, true
case "warn", "warning":
return Warn, true
case "error":
return Error, true
}
return Level(999), false
}
// String converts a Level to string.
func (l Level) String() string {
switch l {
case Trace:
return "trace"
case Debug:
return "debug"
case Info:
return "info"
case Warn:
return "warn"
case Error:
return "error"
}
return "unknown"
}
// UnmarshalText implements encoding.TextUnmarshaler.
func (l *Level) UnmarshalText(text []byte) error {
level, ok := ParseLevel(string(text))
if !ok {
return fmt.Errorf("undefined level: %q", string(text))
}
*l = level
return nil
}
// LevelFunc returns a logger function for a level.
// If the level is invalid it falls back to Info level.
func LevelFunc(logger Logger, level Level) LogFunc {
switch level {
case Trace:
return logger.Trace
case Debug:
return logger.Debug
case Info:
return logger.Info
case Warn:
return logger.Warn
case Error:
return logger.Error
default:
return logger.Info
}
}
// LevelEnabler checks if a level is enabled in a logger.
// If the logger cannot reliably decide the correct level this method MUST return true.
type LevelEnabler interface {
// LevelEnabled checks if a level is enabled in a logger.
LevelEnabled(level Level) bool
}