-
Notifications
You must be signed in to change notification settings - Fork 1
/
level.go
88 lines (80 loc) · 1.8 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
/*
Package level defines a Priority type and some conversion methods for a 7-tiered
logging level schema, which mirror syslog and system's logging levels.
Levels range from Emergency (0) to Debug (7), and the special type
Priority and associated constants provide access to these values.
*/
package level
import "strings"
// Priority is an integer that tracks log levels. Use with one of the
// defined constants.
type Priority int16
// Constants defined for easy access to
const (
Emergency Priority = 100
Alert Priority = 90
Critical Priority = 80
Error Priority = 70
Warning Priority = 60
Notice Priority = 50
Info Priority = 40
Debug Priority = 30
Trace Priority = 20
Invalid Priority = 0
)
// String implements the Stringer interface and makes it possible to
// print human-readable string identifier for a log level.
func (p Priority) String() string {
switch p {
case 100:
return "emergency"
case 90:
return "alert"
case 80:
return "critical"
case 70:
return "error"
case 60:
return "warning"
case 50:
return "notice"
case 40:
return "info"
case 30:
return "debug"
case 20:
return "trace"
default:
return "invalid"
}
}
// IsValid returns false when the priority valid is not a valid
// priority value.
func (p Priority) IsValid() bool {
return p > 1 && p <= 100
}
// FromString takes a string, (case insensitive, leading and trailing space removed, )
func FromString(l string) Priority {
switch strings.TrimSpace(strings.ToLower(l)) {
case "emergency":
return Emergency
case "alert":
return Alert
case "critical":
return Critical
case "error":
return Error
case "warning":
return Warning
case "notice":
return Notice
case "info":
return Info
case "debug":
return Debug
case "trace":
return Trace
default:
return Invalid
}
}