-
Notifications
You must be signed in to change notification settings - Fork 1
/
level.go
115 lines (100 loc) · 1.83 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package logy
import (
"encoding/json"
"gopkg.in/yaml.v3"
"strings"
)
type syslogLevel int
const (
syslogLevelEmergency syslogLevel = iota
syslogLevelAlert
syslogLevelCritical
syslogLevelError
syslogLevelWarning
syslogLevelNotice
syslogLevelInformational
syslogLevelDebug
)
type Level int
var (
levelValues = map[string]Level{
"TRACE": LevelTrace,
"DEBUG": LevelDebug,
"INFO": LevelInfo,
"WARN": LevelWarn,
"ERROR": LevelError,
"ALL": LevelAll,
"OFF": LevelOff,
}
)
const (
LevelOff Level = iota + 1
LevelError
LevelWarn
LevelInfo
LevelDebug
LevelTrace
LevelAll
)
func (l Level) syslogLevel() syslogLevel {
switch l {
case LevelTrace:
return syslogLevelNotice
case LevelDebug:
return syslogLevelDebug
case LevelInfo:
return syslogLevelInformational
case LevelWarn:
return syslogLevelWarning
default:
return syslogLevelError
}
}
func (l Level) String() string {
switch l {
case LevelTrace:
return "TRACE"
case LevelDebug:
return "DEBUG"
case LevelInfo:
return "INFO"
case LevelWarn:
return "WARN"
case LevelError:
return "ERROR"
case LevelOff:
return "OFF"
default:
return "ALL"
}
}
func (l *Level) MarshalJSON() ([]byte, error) {
var builder strings.Builder
builder.WriteByte('"')
builder.WriteString(l.String())
builder.WriteByte('"')
return []byte(builder.String()), nil
}
func (l Level) MarshalYAML() (interface{}, error) {
return l.String(), nil
}
func (l *Level) UnmarshalYAML(node *yaml.Node) error {
if val, ok := levelValues[strings.ToUpper(node.Value)]; ok {
*l = val
} else {
*l = LevelAll
}
return nil
}
func (l *Level) UnmarshalJSON(data []byte) error {
var level string
if err := json.Unmarshal(data, &level); err != nil {
return err
}
if val, ok := levelValues[strings.ToUpper(level)]; ok {
*l = val
} else {
*l = LevelAll
}
return nil
}