-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler.go
More file actions
76 lines (63 loc) · 1.89 KB
/
Copy pathhandler.go
File metadata and controls
76 lines (63 loc) · 1.89 KB
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
package roachslog
import (
"context"
"log/slog"
"github.com/cockroachdb/errors"
)
const defaultStactraceAttrKey = "stacktrace"
type (
// RoachSlogHandler is the slog handler to format stacktrace of logs cockroachdb/errors.
RoachSlogHandler struct {
handler slog.Handler
stacktraceKey string
}
opt func(*RoachSlogHandler)
)
// NewReachSlogHandler creates a new RoachSlogHandler instance.
func NewReachSlogHandler(handler slog.Handler, opts ...opt) slog.Handler {
rs := &RoachSlogHandler{
handler: handler,
stacktraceKey: defaultStactraceAttrKey,
}
for _, option := range opts {
option(rs)
}
return rs
}
func WithStacktraceAttrKey(key string) opt {
return func(rs *RoachSlogHandler) {
rs.stacktraceKey = key
}
}
func (rs *RoachSlogHandler) Enabled(ctx context.Context, level slog.Level) bool {
return rs.handler.Enabled(ctx, level)
}
func (rs *RoachSlogHandler) Handle(ctx context.Context, record slog.Record) error {
var stacktrace string
record.Attrs(func(attr slog.Attr) bool {
if attr.Key == errAttrKey {
// If the cast to error data type fails, no information about the error is logged.
// The value of the error data type should be available as the value corresponding to the specified key (errAttrKey).
if err, ok := attr.Value.Any().(error); ok {
stacktrace = extractStacktrace(err)
record.AddAttrs(slog.String(rs.stacktraceKey, stacktrace))
}
return false
}
return true
})
return rs.handler.Handle(ctx, record)
}
func (rs *RoachSlogHandler) WithAttrs(attrs []slog.Attr) slog.Handler {
return &RoachSlogHandler{handler: rs.handler.WithAttrs(attrs)}
}
func (rs *RoachSlogHandler) WithGroup(g string) slog.Handler {
return &RoachSlogHandler{handler: rs.handler.WithGroup(g)}
}
func extractStacktrace(err error) string {
safeDetails := errors.GetSafeDetails(err).SafeDetails
if len(safeDetails) > 0 {
return safeDetails[0]
}
return ""
}