This repository has been archived by the owner on Jun 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
severity.go
163 lines (144 loc) · 4.19 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package rec
import (
"fmt"
"strconv"
)
// Severity controls the severity of the log output by `*rec.Logger`.
// By default, the following severities are available: `DEFAULT`, `DEBUG`, `INFO`, `NOTICE`, `WARNING`, `ERROR`, `CRITICAL`, `ALERT`, and `EMERGENCY`.
type Severity int
type severityStrings struct {
lowercase string
uppercase string
}
// cf. https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry#logseverity
const (
DEFAULT Severity = iota
DEBUG Severity = 100
INFO Severity = 200
NOTICE Severity = 300
WARNING Severity = 400
ERROR Severity = 500
CRITICAL Severity = 600
ALERT Severity = 700
EMERGENCY Severity = 800
)
const (
lowercaseDefault = "default"
lowercaseDebug = "debug"
lowercaseInfo = "info"
lowercaseNotice = "notice"
lowercaseWarning = "warning"
lowercaseError = "error"
lowercaseCritical = "critical"
lowercaseAlert = "alert"
lowercaseEmergency = "emergency"
)
const (
uppercaseDefault = "DEFAULT"
uppercaseDebug = "DEBUG"
uppercaseInfo = "INFO"
uppercaseNotice = "NOTICE"
uppercaseWarning = "WARNING"
uppercaseError = "ERROR"
uppercaseCritical = "CRITICAL"
uppercaseAlert = "ALERT"
uppercaseEmergency = "EMERGENCY"
)
// lowercase returns the lowercase string of Severity.
// nolint: cyclop
func (l *Logger) lowercase(severity Severity) string {
switch severity {
case DEFAULT:
return lowercaseDefault
case DEBUG:
return lowercaseDebug
case INFO:
return lowercaseInfo
case NOTICE:
return lowercaseNotice
case WARNING:
return lowercaseWarning
case ERROR:
return lowercaseError
case CRITICAL:
return lowercaseCritical
case ALERT:
return lowercaseAlert
case EMERGENCY:
return lowercaseEmergency
default:
if l != nil && l.customSeverities != nil && l.customSeverities[severity] != nil {
return l.customSeverities[severity].lowercase
}
return strconv.Itoa(int(severity))
}
}
// uppercase returns the uppercase string of Severity.
// nolint: cyclop
func (l *Logger) uppercase(severity Severity) string {
switch severity {
case DEFAULT:
return uppercaseDefault
case DEBUG:
return uppercaseDebug
case INFO:
return uppercaseInfo
case NOTICE:
return uppercaseNotice
case WARNING:
return uppercaseWarning
case ERROR:
return uppercaseError
case CRITICAL:
return uppercaseCritical
case ALERT:
return uppercaseAlert
case EMERGENCY:
return uppercaseEmergency
default:
if l != nil && l.customSeverities != nil && l.customSeverities[severity] != nil {
return l.customSeverities[severity].uppercase
}
return strconv.Itoa(int(severity))
}
}
func defaultSeverities() map[Severity]*severityStrings {
return map[Severity]*severityStrings{
DEFAULT: {lowercase: lowercaseDefault, uppercase: uppercaseDefault},
DEBUG: {lowercase: lowercaseDebug, uppercase: uppercaseDebug},
INFO: {lowercase: lowercaseInfo, uppercase: uppercaseInfo},
NOTICE: {lowercase: lowercaseNotice, uppercase: uppercaseNotice},
WARNING: {lowercase: lowercaseWarning, uppercase: uppercaseWarning},
ERROR: {lowercase: lowercaseError, uppercase: uppercaseError},
CRITICAL: {lowercase: lowercaseCritical, uppercase: uppercaseCritical},
ALERT: {lowercase: lowercaseAlert, uppercase: uppercaseAlert},
EMERGENCY: {lowercase: lowercaseEmergency, uppercase: uppercaseEmergency},
}
}
// AddCustomSeverity is a method to add your own Severity to the `*rec.Logger`.
func (l *Logger) AddCustomSeverity(severity Severity, lowercase, uppercase string) error {
l.Lock()
defer l.Unlock()
for customSeverity, severityStrings := range l.customSeverities {
if severity == customSeverity {
return fmt.Errorf("severity=%d: %w", severity, ErrSeverityAlreadyExists)
}
if lowercase == severityStrings.lowercase {
return fmt.Errorf("lowercase=%s: %w", lowercase, ErrSeverityAlreadyExists)
}
if uppercase == severityStrings.uppercase {
return fmt.Errorf("uppercase=%s: %w", uppercase, ErrSeverityAlreadyExists)
}
if lowercase == "" {
return ErrSeverityLowerCaseIsEmpty
}
if uppercase == "" {
return ErrSeverityUpperCaseIsEmpty
}
}
l.customSeverities[severity] = &severityStrings{
uppercase: uppercase,
lowercase: lowercase,
}
return nil
}