-
Notifications
You must be signed in to change notification settings - Fork 4
/
level.go
61 lines (52 loc) · 1.21 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
package log
import (
"strings"
)
// Level is a log level
type Level int
//noinspection GoUnusedConst
// The different log levels
const (
LevelTrace Level = iota
LevelDebug
LevelInfo
LevelWarn
LevelError
LevelFatal
LevelOff
)
// FullName returns the full name of a log level
func (l Level) FullName() string {
return LevelNames[l].Full
}
// ShortName returns the short name of a log level
func (l Level) ShortName() string {
return LevelNames[l].Short
}
// LevelNames contains the full and short names of the log levels
var LevelNames = map[Level]LevelName{
LevelTrace: {"TRACE", "TRC"},
LevelDebug: {"DEBUG", "DBG"},
LevelInfo: {"INFO", "INF"},
LevelWarn: {"WARN", "WRN"},
LevelError: {"ERROR", "ERR"},
LevelFatal: {"FATAL", "FTL"},
}
// LevelName holds the full and short name of a log level
type LevelName struct {
Full string
Short string
}
func shouldLog(level, cutoff Level) bool {
return cutoff <= level
}
// LevelByName retrieves the level with the given name (defaults to LevelOff)
func LevelByName(name string) Level {
name = strings.ToLower(name)
for l, n := range LevelNames {
if strings.ToLower(n.Full) == name || strings.ToLower(n.Short) == name {
return l
}
}
return LevelOff
}