Skip to content

Commit c8c96a6

Browse files
committed
fix: should properly parse slog attributes
1 parent 43318e1 commit c8c96a6

File tree

1 file changed

+39
-1
lines changed

1 file changed

+39
-1
lines changed

cmd/brassite/slog_breadcrumbs.go

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,43 @@ func (s *slogSentryBreadcrumbs) Enabled(context.Context, slog.Level) bool {
3333
return true
3434
}
3535

36+
func (s *slogSentryBreadcrumbs) parseAttributeValue(a slog.Attr) (key string, value any) {
37+
key = a.Key
38+
39+
switch a.Value.Kind() {
40+
case slog.KindAny:
41+
value = a.Value.Any()
42+
case slog.KindBool:
43+
value = a.Value.Bool()
44+
case slog.KindDuration:
45+
value = a.Value.Duration().String()
46+
case slog.KindFloat64:
47+
value = a.Value.Float64()
48+
case slog.KindInt64:
49+
value = a.Value.Int64()
50+
case slog.KindString:
51+
value = a.Value.String()
52+
case slog.KindTime:
53+
value = a.Value.Time().String()
54+
case slog.KindUint64:
55+
value = a.Value.Uint64()
56+
case slog.KindGroup:
57+
m := make(map[string]any)
58+
group := a.Value.Group()
59+
for _, b := range group {
60+
k, v := s.parseAttributeValue(b)
61+
m[k] = v
62+
}
63+
64+
value = m
65+
case slog.KindLogValuer:
66+
valuer := a.Value.LogValuer()
67+
b := slog.Any(a.Key, valuer)
68+
_, value = s.parseAttributeValue(b)
69+
}
70+
return
71+
}
72+
3673
// Handle implements slog.Handler.
3774
func (s *slogSentryBreadcrumbs) Handle(ctx context.Context, r slog.Record) error {
3875
if ctx == nil {
@@ -51,7 +88,8 @@ func (s *slogSentryBreadcrumbs) Handle(ctx context.Context, r slog.Record) error
5188

5289
var data = make(map[string]any)
5390
r.Attrs(func(a slog.Attr) bool {
54-
data[a.Key] = a.Value
91+
key, value := s.parseAttributeValue(a)
92+
data[key] = value
5593
return true
5694
})
5795

0 commit comments

Comments
 (0)