forked from nownabe/clog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoperation.go
43 lines (32 loc) · 946 Bytes
/
operation.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
package clog
import (
"context"
"log/slog"
"go.nownabe.dev/clog/internal/keys"
)
type ctxKeyOperation struct{}
type operation struct {
id string
producer string
}
type operationHandler struct {
slog.Handler
}
func newOperationHandler(h slog.Handler) slog.Handler {
return &operationHandler{h}
}
func (h *operationHandler) Enabled(ctx context.Context, level slog.Level) bool {
return h.Handler.Enabled(ctx, level)
}
func (h *operationHandler) Handle(ctx context.Context, r slog.Record) error {
if op, ok := ctx.Value(ctxKeyOperation{}).(*operation); ok {
r.AddAttrs(slog.Group(keys.Operation, "id", op.id, "producer", op.producer))
}
return h.Handler.Handle(ctx, r)
}
func (h *operationHandler) WithAttrs(attrs []slog.Attr) slog.Handler {
return &operationHandler{h.Handler.WithAttrs(attrs)}
}
func (h *operationHandler) WithGroup(group string) slog.Handler {
return &operationHandler{h.Handler.WithGroup(group)}
}