Skip to content

Commit 9bc5ec6

Browse files
committed
feat: adding HandleInlineHandler and InlineHandler
1 parent 6b959c1 commit 9bc5ec6

File tree

2 files changed

+137
-0
lines changed

2 files changed

+137
-0
lines changed

handler_inline.go

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package slogmulti
2+
3+
import (
4+
"context"
5+
6+
"log/slog"
7+
)
8+
9+
// NewInlineHandler is a shortcut to a handler that implements all methods.
10+
func NewInlineHandler(
11+
enabledFunc func(ctx context.Context, groups []string, attrs []slog.Attr, level slog.Level) bool,
12+
handleFunc func(ctx context.Context, groups []string, attrs []slog.Attr, record slog.Record) error,
13+
) slog.Handler {
14+
return &InlineHandler{
15+
groups: []string{},
16+
attrs: []slog.Attr{},
17+
enabledFunc: enabledFunc,
18+
handleFunc: handleFunc,
19+
}
20+
}
21+
22+
var _ slog.Handler = (*InlineHandler)(nil)
23+
24+
type InlineHandler struct {
25+
groups []string
26+
attrs []slog.Attr
27+
enabledFunc func(ctx context.Context, groups []string, attrs []slog.Attr, level slog.Level) bool
28+
handleFunc func(ctx context.Context, groups []string, attrs []slog.Attr, record slog.Record) error
29+
}
30+
31+
// Implements slog.Handler
32+
func (h *InlineHandler) Enabled(ctx context.Context, level slog.Level) bool {
33+
return h.enabledFunc(ctx, h.groups, h.attrs, level)
34+
}
35+
36+
// Implements slog.Handler
37+
func (h *InlineHandler) Handle(ctx context.Context, record slog.Record) error {
38+
return h.handleFunc(ctx, h.groups, h.attrs, record)
39+
}
40+
41+
// Implements slog.Handler
42+
func (h *InlineHandler) WithAttrs(attrs []slog.Attr) slog.Handler {
43+
newAttrs := []slog.Attr{}
44+
newAttrs = append(newAttrs, h.attrs...)
45+
newAttrs = append(newAttrs, attrs...)
46+
47+
return &InlineHandler{
48+
groups: h.groups,
49+
attrs: newAttrs,
50+
enabledFunc: h.enabledFunc,
51+
handleFunc: h.handleFunc,
52+
}
53+
}
54+
55+
// Implements slog.Handler
56+
func (h *InlineHandler) WithGroup(name string) slog.Handler {
57+
// https://cs.opensource.google/go/x/exp/+/46b07846:slog/handler.go;l=247
58+
if name == "" {
59+
return h
60+
}
61+
62+
newGroups := []string{}
63+
newGroups = append(newGroups, h.groups...)
64+
newGroups = append(newGroups, name)
65+
return &InlineHandler{
66+
groups: newGroups,
67+
attrs: h.attrs,
68+
enabledFunc: h.enabledFunc,
69+
handleFunc: h.handleFunc,
70+
}
71+
}

handler_inline_handle.go

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package slogmulti
2+
3+
import (
4+
"context"
5+
6+
"log/slog"
7+
)
8+
9+
// NewHandleInlineHandler is a shortcut to a middleware that implements only the `Handle` method.
10+
func NewHandleInlineHandler(handleFunc func(ctx context.Context, groups []string, attrs []slog.Attr, record slog.Record) error) Middleware {
11+
return func(next slog.Handler) slog.Handler {
12+
return &HandleInlineHandler{
13+
groups: []string{},
14+
attrs: []slog.Attr{},
15+
handleFunc: handleFunc,
16+
}
17+
}
18+
}
19+
20+
var _ slog.Handler = (*HandleInlineHandler)(nil)
21+
22+
type HandleInlineHandler struct {
23+
groups []string
24+
attrs []slog.Attr
25+
handleFunc func(ctx context.Context, groups []string, attrs []slog.Attr, record slog.Record) error
26+
}
27+
28+
// Implements slog.Handler
29+
func (h *HandleInlineHandler) Enabled(ctx context.Context, level slog.Level) bool {
30+
return true
31+
}
32+
33+
// Implements slog.Handler
34+
func (h *HandleInlineHandler) Handle(ctx context.Context, record slog.Record) error {
35+
return h.handleFunc(ctx, h.groups, h.attrs, record)
36+
}
37+
38+
// Implements slog.Handler
39+
func (h *HandleInlineHandler) WithAttrs(attrs []slog.Attr) slog.Handler {
40+
newAttrs := []slog.Attr{}
41+
newAttrs = append(newAttrs, h.attrs...)
42+
newAttrs = append(newAttrs, attrs...)
43+
44+
return &InlineHandler{
45+
groups: h.groups,
46+
attrs: newAttrs,
47+
handleFunc: h.handleFunc,
48+
}
49+
}
50+
51+
// Implements slog.Handler
52+
func (h *HandleInlineHandler) WithGroup(name string) slog.Handler {
53+
// https://cs.opensource.google/go/x/exp/+/46b07846:slog/handler.go;l=247
54+
if name == "" {
55+
return h
56+
}
57+
58+
newGroups := []string{}
59+
newGroups = append(newGroups, h.groups...)
60+
newGroups = append(newGroups, name)
61+
return &InlineHandler{
62+
groups: newGroups,
63+
attrs: h.attrs,
64+
handleFunc: h.handleFunc,
65+
}
66+
}

0 commit comments

Comments
 (0)