forked from ihippik/slog-sentry
-
Notifications
You must be signed in to change notification settings - Fork 2
/
handler.go
112 lines (95 loc) · 2.65 KB
/
handler.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package slogsentry
import (
"context"
"fmt"
"log/slog"
"slices"
"github.com/getsentry/sentry-go"
)
const (
shortErrKey = "err"
longErrKey = "error"
)
var slogDefaultKeys = []string{slog.TimeKey, slog.LevelKey, slog.SourceKey, slog.MessageKey, shortErrKey, longErrKey}
// SlogEror contains both the slog msg and the actual error.
type SlogError struct {
msg string
err error
}
// Error appends both the msg and err from the SlogError.
func (e SlogError) Error() string {
msg := e.msg
if e.err != nil {
if len(msg) > 0 {
msg += ": "
}
msg += e.err.Error()
}
return msg
}
func (e SlogError) Unwrap() error {
return e.err
}
// SentryHandler is a Handler that writes log records to the Sentry.
type SentryHandler struct {
slog.Handler
levels []slog.Level
}
// NewSentryHandler creates a SentryHandler that writes to w,
// using the given options.
func NewSentryHandler(
handler slog.Handler,
levels []slog.Level,
) *SentryHandler {
return &SentryHandler{
Handler: handler,
levels: levels,
}
}
// Enabled reports whether the handler handles records at the given level.
func (s *SentryHandler) Enabled(ctx context.Context, level slog.Level) bool {
return s.Handler.Enabled(ctx, level)
}
// Handle intercepts and processes logger messages.
// In our case, send a message to the Sentry.
func (s *SentryHandler) Handle(ctx context.Context, record slog.Record) error {
if slices.Contains(s.levels, record.Level) {
hub := sentry.GetHubFromContext(ctx)
if hub == nil {
hub = sentry.CurrentHub()
}
if hub == nil {
return fmt.Errorf("sentry: hub is nil")
}
var err error
slogContext := map[string]any{}
record.Attrs(func(attr slog.Attr) bool {
if !slices.Contains(slogDefaultKeys, attr.Key) {
slogContext[attr.Key] = attr.Value.String()
} else if attr.Key == shortErrKey || attr.Key == longErrKey {
err = attr.Value.Any().(error)
}
return true
})
hub.WithScope(func(scope *sentry.Scope) {
if len(slogContext) > 0 {
scope.SetContext("slog", slogContext)
}
switch record.Level {
case slog.LevelError:
sentry.CaptureException(SlogError{msg: record.Message, err: err})
case slog.LevelDebug, slog.LevelInfo, slog.LevelWarn:
sentry.CaptureMessage(record.Message)
}
})
}
return s.Handler.Handle(ctx, record)
}
// WithAttrs returns a new SentryHandler whose attributes consists.
func (s *SentryHandler) WithAttrs(attrs []slog.Attr) slog.Handler {
return NewSentryHandler(s.Handler.WithAttrs(attrs), s.levels)
}
// WithGroup returns a new SentryHandler whose group consists.
func (s *SentryHandler) WithGroup(name string) slog.Handler {
return NewSentryHandler(s.Handler.WithGroup(name), s.levels)
}