-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreplace.go
More file actions
58 lines (54 loc) · 1.42 KB
/
Copy pathreplace.go
File metadata and controls
58 lines (54 loc) · 1.42 KB
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
package sloggcp
import (
"log/slog"
"strings"
)
// ReplaceAttr replaces slog default attributes with GCP compatible ones
// https://cloud.google.com/logging/docs/structured-logging
// https://cloud.google.com/logging/docs/agent/logging/configuration#special-fields
func ReplaceAttr(groups []string, a slog.Attr) slog.Attr {
// only handle top-level attributes
if len(groups) > 0 {
return a
}
switch {
case a.Key == slog.LevelKey:
return replaceLevelAttr(a)
case a.Key == slog.SourceKey:
a.Key = SourceLocationKey
case a.Key == slog.MessageKey:
a.Key = MessageKey
case a.Key == slog.TimeKey:
// no replacement needed
case strings.EqualFold(a.Key, "TraceID"):
a.Key = TraceKey
case strings.EqualFold(a.Key, "SpanID"):
a.Key = SpanIDKey
}
return a
}
var (
severityDebug = slog.String(SeverityKey, DebugSeverity)
severityInfo = slog.String(SeverityKey, InfoSeverity)
severityWarn = slog.String(SeverityKey, WarningSeverity)
severityError = slog.String(SeverityKey, ErrorSeverity)
severityDefault = slog.String(SeverityKey, DefaultSeverity)
)
func replaceLevelAttr(a slog.Attr) slog.Attr {
logLevel, ok := a.Value.Any().(slog.Level)
if !ok {
return severityDefault
}
switch logLevel {
case slog.LevelDebug:
return severityDebug
case slog.LevelInfo:
return severityInfo
case slog.LevelWarn:
return severityWarn
case slog.LevelError:
return severityError
default:
return severityDefault
}
}