-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
converter.go
48 lines (36 loc) · 1.23 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
41
42
43
44
45
46
47
48
package slogmicrosoftteams
import (
"fmt"
"log/slog"
slogcommon "github.com/samber/slog-common"
)
var SourceKey = "source"
type Converter func(addSource bool, replaceAttr func(groups []string, a slog.Attr) slog.Attr, loggerAttr []slog.Attr, groups []string, record *slog.Record) string
func DefaultConverter(addSource bool, replaceAttr func(groups []string, a slog.Attr) slog.Attr, loggerAttr []slog.Attr, groups []string, record *slog.Record) string {
// aggregate all attributes
attrs := slogcommon.AppendRecordAttrsToAttrs(loggerAttr, groups, record)
// developer formatters
if addSource {
attrs = append(attrs, slogcommon.Source(SourceKey, record))
}
attrs = slogcommon.ReplaceAttrs(replaceAttr, []string{}, attrs...)
attrs = slogcommon.RemoveEmptyAttrs(attrs)
// handler formatter
message := attrToTeamsMessage("", attrs)
return message
}
func attrToTeamsMessage(base string, attrs []slog.Attr) string {
message := ""
for i := range attrs {
attr := attrs[i]
k := base + attr.Key
v := attr.Value
kind := attr.Value.Kind()
if kind == slog.KindGroup {
message += attrToTeamsMessage(k+".", v.Group())
} else {
message += fmt.Sprintf("**%s**: %s\n\n", k, slogcommon.ValueToString(v))
}
}
return message
}