-
Notifications
You must be signed in to change notification settings - Fork 0
/
level.go
61 lines (53 loc) · 1.08 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 gologger
import (
"fmt"
"github.com/pkg/errors"
"strings"
)
type Level string
const (
LevelDebug Level = "debug"
LevelInfo Level = "info"
LevelWarning Level = "warning"
LevelError Level = "error"
LevelFatal Level = "fatal"
)
var levelMap = map[Level]int{
LevelDebug: 100,
LevelInfo: 200,
LevelWarning: 300,
LevelError: 400,
LevelFatal: 500,
}
func ParseLevel(lvl string) (Level, error) {
switch strings.ToLower(lvl) {
case "fatal":
return LevelFatal, nil
case "error":
return LevelError, nil
case "warn", "warning":
return LevelWarning, nil
case "info":
return LevelInfo, nil
case "debug":
return LevelDebug, nil
}
return Level(""), fmt.Errorf("not a valid log level: %q", lvl)
}
func (l Level) String() string {
return string(l)
}
func (l Level) isGTE(t Level) bool {
v1, ok := levelMap[l]
if ok == false {
panic(errors.Errorf("invalid source level: %s", l))
}
v2, ok := levelMap[t]
if ok == false {
panic(errors.Errorf("invalid targer level: %s", t))
}
return v1 >= v2
}
func (l Level) isLower(t Level) bool {
return !l.isGTE(t)
}