-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.go
43 lines (38 loc) · 972 Bytes
/
logger.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
package app
import (
"os"
"strconv"
"log/slog"
)
func SetupLogger() {
opts := &slog.HandlerOptions{
Level: slog.LevelInfo,
ReplaceAttr: cloudLoggingAttributes(),
}
if v, _ := strconv.ParseBool(os.Getenv("DEBUG")); v {
opts.Level = slog.LevelDebug
}
logger := slog.New(slog.NewJSONHandler(os.Stderr, opts))
slog.SetDefault(logger)
}
var (
cloudLoggingSeverityError = "ERROR"
cloudLoggingSeverityWarning = "WARNING"
cloudLoggingSeverityInfo = "INFO"
cloudLoggingSeverityDebug = "DEBUG"
)
func cloudLoggingAttributes() func([]string, slog.Attr) slog.Attr {
levelToSeverity := map[string]string{
"ERROR": cloudLoggingSeverityError,
"WARN": cloudLoggingSeverityWarning,
"INFO": cloudLoggingSeverityInfo,
"DEBUG": cloudLoggingSeverityDebug,
}
return func(groups []string, a slog.Attr) slog.Attr {
if a.Key == "level" {
a.Key = "severity"
a.Value = slog.StringValue(levelToSeverity[a.Value.String()])
}
return a
}
}