Skip to content

Commit bfcc633

Browse files
committed
refactor: rename MultiHandler to FanoutHandler
1 parent e9adeb4 commit bfcc633

File tree

4 files changed

+16
-18
lines changed

4 files changed

+16
-18
lines changed

README.md

+6-4
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ No breaking changes will be made to exported APIs before v1.0.0.
4545

4646
GoDoc: [https://pkg.go.dev/github.com/samber/slog-multi](https://pkg.go.dev/github.com/samber/slog-multi)
4747

48-
### Fanout: `slogmulti.Multi()`
48+
### Broadcast: `slogmulti.Fanout()`
4949

5050
Distribute logs to multiple `slog.Handler` in parallel.
5151

@@ -60,9 +60,9 @@ func main() {
6060
stderr := os.Stderr
6161

6262
logger := slog.New(
63-
slogmulti.NewMultiHandler(
64-
slog.HandlerOptions{}.NewJSONHandler(logstash), // first handler: logstash over tcp
65-
slog.HandlerOptions{}.NewTextHandler(stderr), // second handler: stderr
63+
slogmulti.Fanout(
64+
slog.HandlerOptions{}.NewJSONHandler(logstash), // pass to first handler: logstash over tcp
65+
slog.HandlerOptions{}.NewTextHandler(stderr), // then to second handler: stderr
6666
// ...
6767
),
6868
)
@@ -248,12 +248,14 @@ import (
248248
"golang.org/x/exp/slog"
249249
)
250250

251+
251252
func main() {
252253
// ncat -l 1000 -k
253254
// ncat -l 1001 -k
254255
// ncat -l 1002 -k
255256

256257
// list AZs
258+
// use github.com/netbrain/goautosocket for auto-reconnect
257259
logstash1, _ := net.Dial("tcp", "logstash.eu-west-3a.internal:1000")
258260
logstash2, _ := net.Dial("tcp", "logstash.eu-west-3b.internal:1000")
259261
logstash3, _ := net.Dial("tcp", "logstash.eu-west-3c.internal:1000")

examples/either/example.go

-4
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,6 @@ func main() {
1414
// ncat -l 1001 -k
1515
// ncat -l 1002 -k
1616

17-
// logstash1, err := gas.Dial("tcp", "logstash.eu-west-3a.internal:1000")
18-
// logstash2, err := gas.Dial("tcp", "logstash.eu-west-3b.internal:1000")
19-
// logstash3, err := gas.Dial("tcp", "logstash.eu-west-3c.internal:1000")
20-
2117
logstash1, _ := net.Dial("tcp", "localhost:1000")
2218
logstash2, _ := net.Dial("tcp", "localhost:1001")
2319
logstash3, _ := net.Dial("tcp", "localhost:1002")

examples/multi/example.go renamed to examples/fanout/example.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func main() {
3131
stderr := os.Stderr
3232

3333
logger := slog.New(
34-
slogmulti.NewMultiHandler(
34+
slogmulti.Fanout(
3535
slog.HandlerOptions{}.NewJSONHandler(logstash),
3636
slog.HandlerOptions{}.NewTextHandler(stderr),
3737
),

multi.go

+9-9
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,17 @@ import (
77
"golang.org/x/exp/slog"
88
)
99

10-
type MultiHandler struct {
10+
type FanoutHandler struct {
1111
handlers []slog.Handler
1212
}
1313

14-
func NewMultiHandler(handlers ...slog.Handler) slog.Handler {
15-
return &MultiHandler{
14+
func Fanout(handlers ...slog.Handler) slog.Handler {
15+
return &FanoutHandler{
1616
handlers: handlers,
1717
}
1818
}
1919

20-
func (h *MultiHandler) Enabled(ctx context.Context, l slog.Level) bool {
20+
func (h *FanoutHandler) Enabled(ctx context.Context, l slog.Level) bool {
2121
for i := range h.handlers {
2222
if h.handlers[i].Enabled(ctx, l) {
2323
return true
@@ -28,7 +28,7 @@ func (h *MultiHandler) Enabled(ctx context.Context, l slog.Level) bool {
2828
}
2929

3030
// @TODO: return multiple errors ?
31-
func (h *MultiHandler) Handle(ctx context.Context, r slog.Record) error {
31+
func (h *FanoutHandler) Handle(ctx context.Context, r slog.Record) error {
3232
for i := range h.handlers {
3333
if h.handlers[i].Enabled(ctx, r.Level) {
3434
err := try(func() error {
@@ -43,16 +43,16 @@ func (h *MultiHandler) Handle(ctx context.Context, r slog.Record) error {
4343
return nil
4444
}
4545

46-
func (h *MultiHandler) WithAttrs(attrs []slog.Attr) slog.Handler {
46+
func (h *FanoutHandler) WithAttrs(attrs []slog.Attr) slog.Handler {
4747
handers := lo.Map(h.handlers, func(h slog.Handler, _ int) slog.Handler {
4848
return h.WithAttrs(attrs)
4949
})
50-
return NewMultiHandler(handers...)
50+
return Fanout(handers...)
5151
}
5252

53-
func (h *MultiHandler) WithGroup(name string) slog.Handler {
53+
func (h *FanoutHandler) WithGroup(name string) slog.Handler {
5454
handers := lo.Map(h.handlers, func(h slog.Handler, _ int) slog.Handler {
5555
return h.WithGroup(name)
5656
})
57-
return NewMultiHandler(handers...)
57+
return Fanout(handers...)
5858
}

0 commit comments

Comments
 (0)