-
Notifications
You must be signed in to change notification settings - Fork 21
/
level.go
91 lines (83 loc) · 1.51 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
package log
import (
"bytes"
"strings"
)
// AllLevels is an array of all log levels, for easier registering of all levels to a handler
var AllLevels = []Level{
DebugLevel,
InfoLevel,
NoticeLevel,
WarnLevel,
ErrorLevel,
PanicLevel,
AlertLevel,
FatalLevel,
}
// Level of the log
type Level uint8
// Log levels.
const (
DebugLevel Level = iota
InfoLevel
NoticeLevel
WarnLevel
ErrorLevel
PanicLevel
AlertLevel
FatalLevel // same as syslog CRITICAL
)
func (l Level) String() string {
switch l {
case DebugLevel:
return "DEBUG"
case InfoLevel:
return "INFO"
case NoticeLevel:
return "NOTICE"
case WarnLevel:
return "WARN"
case ErrorLevel:
return "ERROR"
case PanicLevel:
return "PANIC"
case AlertLevel:
return "ALERT"
case FatalLevel:
return "FATAL"
default:
return "Unknown Level"
}
}
// ParseLevel parses the provided strings log level or if not supported return 255
func ParseLevel(s string) Level {
switch strings.ToUpper(s) {
case "DEBUG":
return DebugLevel
case "INFO":
return InfoLevel
case "NOTICE":
return NoticeLevel
case "WARN":
return WarnLevel
case "ERROR":
return ErrorLevel
case "PANIC":
return PanicLevel
case "ALERT":
return AlertLevel
case "FATAL":
return FatalLevel
default:
return 255
}
}
// MarshalJSON implementation.
func (l Level) MarshalJSON() ([]byte, error) {
return []byte(`"` + l.String() + `"`), nil
}
// UnmarshalJSON implementation.
func (l *Level) UnmarshalJSON(b []byte) error {
*l = ParseLevel(string(bytes.Trim(b, `"`)))
return nil
}