-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconverter.go
40 lines (31 loc) · 1.19 KB
/
converter.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
package slogbetterstack
import (
"log/slog"
slogcommon "github.com/samber/slog-common"
)
var SourceKey = "runtime"
var ContextKey = "extra"
var ErrorKeys = []string{"error", "err"}
type Converter func(addSource bool, replaceAttr func(groups []string, a slog.Attr) slog.Attr, loggerAttr []slog.Attr, groups []string, record *slog.Record) map[string]any
func DefaultConverter(addSource bool, replaceAttr func(groups []string, a slog.Attr) slog.Attr, loggerAttr []slog.Attr, groups []string, record *slog.Record) map[string]any {
// aggregate all attributes
attrs := slogcommon.AppendRecordAttrsToAttrs(loggerAttr, groups, record)
// developer formatters
attrs = slogcommon.ReplaceError(attrs, ErrorKeys...)
if addSource {
attrs = append(attrs, slogcommon.Source(SourceKey, record))
}
attrs = slogcommon.ReplaceAttrs(replaceAttr, []string{}, attrs...)
attrs = slogcommon.RemoveEmptyAttrs(attrs)
// handler formatter
ctx := slogcommon.AttrsToMap(attrs...)
payload := map[string]any{
"logger.name": name,
"logger.version": version,
"dt": record.Time.UTC(),
"level": record.Level.String(),
"message": record.Message,
ContextKey: ctx,
}
return payload
}