-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler.go
More file actions
47 lines (39 loc) · 862 Bytes
/
handler.go
File metadata and controls
47 lines (39 loc) · 862 Bytes
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
package slslog
import (
"context"
"fmt"
"log/slog"
"os"
)
type slsLogHandler struct {
attrs []slog.Attr
groups []string
}
func (h *slsLogHandler) Handle(ctx context.Context, r slog.Record) error {
r.AddAttrs(h.attrs...)
var out string
r.Attrs(func(attr slog.Attr) bool {
if attr.Key != "level" && attr.Key != "msg" && attr.Key != "time" {
out += fmt.Sprintf(`"%s":"%v",`, attr.Key, attr.Value)
}
return true
})
if len(out) > 0 {
out = out[:len(out)-1]
}
fmt.Fprintf(os.Stdout, "{%s}\n", out)
return nil
}
func (h *slsLogHandler) Enabled(context.Context, slog.Level) bool {
return true
}
func (h *slsLogHandler) WithAttrs(attrs []slog.Attr) slog.Handler {
return &slsLogHandler{
attrs: attrs,
}
}
func (h *slsLogHandler) WithGroup(group string) slog.Handler {
return &slsLogHandler{
groups: append(h.groups, group),
}
}