This repository has been archived by the owner on Nov 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
severity.go
101 lines (87 loc) · 2.11 KB
/
severity.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
package xlog
import (
"errors"
"strings"
)
var (
ErrInvalidSeverity = errors.New("invalid severity")
ErrUnknownSeverity = errors.New("unknown severity")
)
// Severity describes severity level of Log.
type Severity int
const (
// SeverityNone is none or unspecified severity level
SeverityNone Severity = iota
// SeverityFatal is fatal severity level
SeverityFatal
// SeverityError is error severity level
SeverityError
// SeverityWarning is warning severity level
SeverityWarning
// SeverityInfo is info severity level
SeverityInfo
// SeverityDebug is debug severity level
SeverityDebug
)
// String is implementation of fmt.Stringer.
func (s Severity) String() string {
text, _ := s.MarshalText()
return string(text)
}
// IsValid returns whether s is valid.
func (s Severity) IsValid() bool {
return s.CheckValid() == nil
}
// CheckValid returns ErrInvalidSeverity for invalid s.
func (s Severity) CheckValid() error {
if !(SeverityNone <= s && s <= SeverityDebug) {
return ErrInvalidSeverity
}
return nil
}
// MarshalText is implementation of encoding.TextMarshaler.
// If s is invalid, it returns nil and result of Severity.CheckValid.
func (s Severity) MarshalText() (text []byte, err error) {
if e := s.CheckValid(); e != nil {
return nil, e
}
var str string
switch s {
case SeverityNone:
str = "NONE"
case SeverityFatal:
str = "FATAL"
case SeverityError:
str = "ERROR"
case SeverityWarning:
str = "WARNING"
case SeverityInfo:
str = "INFO"
case SeverityDebug:
str = "DEBUG"
default:
panic("invalid severity")
}
return []byte(str), nil
}
// UnmarshalText is implementation of encoding.UnmarshalText.
// If text is unknown, it returns ErrUnknownSeverity.
func (s *Severity) UnmarshalText(text []byte) error {
switch str := strings.ToUpper(string(text)); str {
case "NONE", "NON", "NA":
*s = SeverityNone
case "FATAL", "FTL":
*s = SeverityFatal
case "ERROR", "ERR":
*s = SeverityError
case "WARNING", "WRN", "WARN":
*s = SeverityWarning
case "INFO", "INF":
*s = SeverityInfo
case "DEBUG", "DBG":
*s = SeverityDebug
default:
return ErrUnknownSeverity
}
return nil
}