-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreplace.go
29 lines (27 loc) · 906 Bytes
/
replace.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
package clog
import "log/slog"
// ReplaceAttr replaces attr keys and values to fit Google Cloud Logging's
// expected structure as per:
// - https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry
// - https://cloud.google.com/functions/docs/monitoring/logging
// - https://cloud.google.com/functions/docs/monitoring/logging
func ReplaceAttr(groups []string, attr slog.Attr) slog.Attr {
// Based on https://github.com/remko/cloudrun-slog
switch attr.Key {
// This is not strictly necessary, as Cloud Logging supports "time" as well.
// TODO: make configurable.
// case slog.TimeKey:
// attr.Key = TimestampKey
case slog.MessageKey:
attr.Key = MessageKey
case slog.SourceKey:
attr.Key = SourceLocationKey
case slog.LevelKey:
level, ok := attr.Value.Any().(slog.Level)
if ok {
attr.Key = SeverityKey
attr.Value = slog.StringValue(LevelName(level))
}
}
return attr
}