-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlevel.go
136 lines (115 loc) · 3.11 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
package logf
import (
"fmt"
"strings"
)
// Level defines severity level of a log message.
type Level int8
// Severity levels.
const (
// LevelError allows to log errors only.
LevelError Level = iota
// LevelWarn allows to log errors and warnings.
LevelWarn
// LevelInfo is the default logging level. Allows to log errors, warnings and infos.
LevelInfo
// LevelDebug allows to log messages with all severity levels.
LevelDebug
)
// Checker is the common way to get LevelChecker. Use it with every custom
// implementation of Level.
func (l Level) Checker() LevelChecker {
return func(o Level) bool {
return l.Enabled(o)
}
}
// LevelChecker implements LevelCheckerGetter.
func (l Level) LevelChecker() LevelChecker {
return l.Checker()
}
// Enabled returns true if the given level is allowed within the current level.
func (l Level) Enabled(o Level) bool {
return l >= o
}
// String implements fmt.Stringer.
// String returns a lower-case string representation of the Level.
func (l Level) String() string {
switch l {
case LevelDebug:
return "debug"
case LevelInfo:
return "info"
case LevelWarn:
return "warn"
case LevelError:
return "error"
default:
return "unknown"
}
}
// UpperCaseString returns an upper-case string representation of the Level.
func (l Level) UpperCaseString() string {
switch l {
case LevelDebug:
return "DEBUG"
case LevelInfo:
return "INFO"
case LevelWarn:
return "WARN"
case LevelError:
return "ERROR"
default:
return "UNKNOWN"
}
}
// MarshalText marshals the Level to text.
func (l Level) MarshalText() ([]byte, error) {
return []byte(l.String()), nil
}
// UnmarshalText unmarshals the Level from text.
func (l *Level) UnmarshalText(text []byte) error {
s := string(text)
lvl, ok := LevelFromString(s)
if !ok {
return fmt.Errorf("invalid logging level %q", s)
}
*l = lvl
return nil
}
// LevelFromString creates the new Level with the given string.
func LevelFromString(lvl string) (Level, bool) {
switch strings.ToLower(lvl) {
case "debug":
return LevelDebug, true
case "info", "information":
return LevelInfo, true
case "warn", "warning":
return LevelWarn, true
case "error":
return LevelError, true
}
return LevelError, false
}
// LevelChecker abstracts level checking process.
type LevelChecker func(Level) bool
// LevelCheckerGetter allows the implementor to act like a common Level
// checker for the Logger.
type LevelCheckerGetter interface {
LevelChecker() LevelChecker
}
// LevelCheckerGetterFunc defines a function that returns LevelChecker.
type LevelCheckerGetterFunc func() LevelChecker
// LevelChecker implements LevelCheckerGetter interface.
func (fn LevelCheckerGetterFunc) LevelChecker() LevelChecker {
return fn()
}
// LevelEncoder is the function type to encode Level.
type LevelEncoder func(Level, TypeEncoder)
// DefaultLevelEncoder implements LevelEncoder by calling Level itself.
func DefaultLevelEncoder(lvl Level, m TypeEncoder) {
m.EncodeTypeString(lvl.String())
}
// UpperCaseLevelEncoder implements LevelEncoder by calling Level itself.
func UpperCaseLevelEncoder(lvl Level, m TypeEncoder) {
m.EncodeTypeString(lvl.UpperCaseString())
}