-
Notifications
You must be signed in to change notification settings - Fork 0
/
levels.go
53 lines (46 loc) · 891 Bytes
/
levels.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
package log
import (
"errors"
)
// Level of severity.
type Level int
// Log levels.
const (
DebugLevel Level = iota
InfoLevel
WarnLevel
ErrorLevel
FatalLevel
)
var levelNames = [...]string{
DebugLevel: "debug",
InfoLevel: "info",
WarnLevel: "warn",
ErrorLevel: "error",
FatalLevel: "fatal",
}
// String implements io.Stringer.
func (l Level) String() string {
return levelNames[l]
}
// MarshalJSON returns the level string.
func (l Level) MarshalJSON() ([]byte, error) {
return []byte(`"` + l.String() + `"`), nil
}
// ParseLevel parses level string.
func ParseLevel(s string) (Level, error) {
switch s {
case "debug":
return DebugLevel, nil
case "info":
return InfoLevel, nil
case "warn", "warning":
return WarnLevel, nil
case "error":
return ErrorLevel, nil
case "fatal":
return FatalLevel, nil
default:
return -1, errors.New("invalid level")
}
}