-
Notifications
You must be signed in to change notification settings - Fork 1
/
levels.go
78 lines (71 loc) · 1.4 KB
/
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
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
package stackdriver
import (
"log/slog"
"strconv"
"strings"
)
// The slog package provides four log levels by default,
// and each one is associated with an integer value:
// DEBUG (-4), INFO (0), WARN (4), and ERROR (8).
const (
DEBUG = slog.LevelDebug
INFO = slog.LevelInfo
NOTICE = slog.Level(2)
WARNING = slog.LevelWarn
ERROR = slog.LevelError
CRITICAL = slog.Level(10)
ALERT = slog.Level(12)
EMERGENCY = slog.Level(14)
)
var levelNames = map[slog.Leveler]string{
NOTICE: "NOTICE",
CRITICAL: "CRITICAL",
ALERT: "ALERT",
EMERGENCY: "EMERGENCY",
}
func ReplaceLogLevel(_ []string, a slog.Attr) slog.Attr {
if a.Key == slog.LevelKey {
level := a.Value.Any().(slog.Level)
value, ok := levelNames[level]
if !ok {
value = level.String()
}
a.Key = "severity"
a.Value = slog.StringValue(value)
return a
}
return a
}
func ParseLogLevel(value string) slog.Leveler {
if len(value) > 0 {
v := strings.ToLower(strings.Trim(value, " "))
if v == "debug" {
return DEBUG
}
if v == "info" {
return INFO
}
if v == "notice" {
return NOTICE
}
if v == "warning" {
return WARNING
}
if v == "error" {
return ERROR
}
if v == "critical" {
return CRITICAL
}
if v == "alert" {
return ALERT
}
if v == "emergency" {
return EMERGENCY
}
if i, err := strconv.Atoi(value); err == nil {
return slog.Level(i)
}
}
return INFO
}