Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 34a36ea

Browse files
7
Change-Id: Icae5ca0615da377ae22a1ae0989f868decbd4fc0
1 parent 9f365b6 commit 34a36ea

File tree

5 files changed

+32
-26
lines changed

5 files changed

+32
-26
lines changed

‎api/next/65954.txt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,6 @@
1-
pkg log/slog, func MultiHandler(...Handler) Handler #65954
1+
pkg log/slog, func NewMultiHandler(...Handler) *MultiHandler #65954
2+
pkg log/slog, method (*MultiHandler) Enabled(context.Context, Level) bool #65954
3+
pkg log/slog, method (*MultiHandler) Handle(context.Context, Record) error #65954
4+
pkg log/slog, method (*MultiHandler) WithAttrs([]Attr) Handler #65954
5+
pkg log/slog, method (*MultiHandler) WithGroup(string) Handler #65954
6+
pkg log/slog, type MultiHandler struct #65954

‎doc/next/6-stdlib/99-minor/log/slog/65954.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
The [`MultiHandler`](/pkg/log/slog#MultiHandler) function returns a handler that
2-
invokes all the given Handlers.
1+
The [`NewMultiHandler`](/pkg/log/slog#NewMultiHandler) function creates a
2+
[`MultiHandler`](/pkg/log/slog#MultiHandler) that invokes all the given Handlers.
33
Its `Enable` method reports whether any of the handlers' `Enabled` methods
44
return true.
55
Its `Handle`, `WithAttr` and `WithGroup` methods call the corresponding method

‎src/log/slog/example_multi_handler_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func ExampleMultiHandler() {
2222
textHandler := slog.NewTextHandler(&textBuf, &slog.HandlerOptions{ReplaceAttr: removeTime})
2323
jsonHandler := slog.NewJSONHandler(&jsonBuf, &slog.HandlerOptions{ReplaceAttr: removeTime})
2424

25-
multiHandler := slog.MultiHandler(textHandler, jsonHandler)
25+
multiHandler := slog.NewMultiHandler(textHandler, jsonHandler)
2626
logger := slog.New(multiHandler)
2727

2828
logger.Info("login",

‎src/log/slog/multi_handler.go

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,21 @@ import (
99
"errors"
1010
)
1111

12-
// MultiHandler returns a handler that invokes all the given Handlers.
13-
// Its Enable method reports whether any of the handlers' Enabled methods return true.
14-
// Its Handle, WithAttr and WithGroup methods call the corresponding method on each of the enabled handlers.
15-
func MultiHandler(handlers ...Handler) Handler {
12+
// NewMultiHandler creates a [MultiHandler] with the given Handlers.
13+
func NewMultiHandler(handlers ...Handler) *MultiHandler {
1614
h := make([]Handler, len(handlers))
1715
copy(h, handlers)
18-
return &multiHandler{multi: h}
16+
return &MultiHandler{multi: h}
1917
}
2018

21-
type multiHandler struct {
19+
// MultiHandler is a [Handler] that invokes all the given Handlers.
20+
// Its Enable method reports whether any of the handlers' Enabled methods return true.
21+
// Its Handle, WithAttr and WithGroup methods call the corresponding method on each of the enabled handlers.
22+
type MultiHandler struct {
2223
multi []Handler
2324
}
2425

25-
func (h *multiHandler) Enabled(ctx context.Context, l Level) bool {
26+
func (h *MultiHandler) Enabled(ctx context.Context, l Level) bool {
2627
for i := range h.multi {
2728
if h.multi[i].Enabled(ctx, l) {
2829
return true
@@ -31,7 +32,7 @@ func (h *multiHandler) Enabled(ctx context.Context, l Level) bool {
3132
return false
3233
}
3334

34-
func (h *multiHandler) Handle(ctx context.Context, r Record) error {
35+
func (h *MultiHandler) Handle(ctx context.Context, r Record) error {
3536
var errs []error
3637
for i := range h.multi {
3738
if h.multi[i].Enabled(ctx, r.Level) {
@@ -43,18 +44,18 @@ func (h *multiHandler) Handle(ctx context.Context, r Record) error {
4344
return errors.Join(errs...)
4445
}
4546

46-
func (h *multiHandler) WithAttrs(attrs []Attr) Handler {
47+
func (h *MultiHandler) WithAttrs(attrs []Attr) Handler {
4748
handlers := make([]Handler, 0, len(h.multi))
4849
for i := range h.multi {
4950
handlers = append(handlers, h.multi[i].WithAttrs(attrs))
5051
}
51-
return &multiHandler{multi: handlers}
52+
return &MultiHandler{multi: handlers}
5253
}
5354

54-
func (h *multiHandler) WithGroup(name string) Handler {
55+
func (h *MultiHandler) WithGroup(name string) Handler {
5556
handlers := make([]Handler, 0, len(h.multi))
5657
for i := range h.multi {
5758
handlers = append(handlers, h.multi[i].WithGroup(name))
5859
}
59-
return &multiHandler{multi: handlers}
60+
return &MultiHandler{multi: handlers}
6061
}

‎src/log/slog/multi_handler_test.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func TestMultiHandler(t *testing.T) {
3030
h1 := NewTextHandler(&buf1, nil)
3131
h2 := NewJSONHandler(&buf2, nil)
3232

33-
multi := MultiHandler(h1, h2)
33+
multi := NewMultiHandler(h1, h2)
3434
logger := New(multi)
3535

3636
logger.Info("hello world", "user", "test")
@@ -43,7 +43,7 @@ func TestMultiHandler(t *testing.T) {
4343
h1 := NewTextHandler(&bytes.Buffer{}, &HandlerOptions{Level: LevelError})
4444
h2 := NewTextHandler(&bytes.Buffer{}, &HandlerOptions{Level: LevelInfo})
4545

46-
multi := MultiHandler(h1, h2)
46+
multi := NewMultiHandler(h1, h2)
4747

4848
if !multi.Enabled(context.Background(), LevelInfo) {
4949
t.Error("Enabled should be true for INFO level, but got false")
@@ -57,7 +57,7 @@ func TestMultiHandler(t *testing.T) {
5757
h1 := NewTextHandler(&bytes.Buffer{}, &HandlerOptions{Level: LevelError})
5858
h2 := NewTextHandler(&bytes.Buffer{}, &HandlerOptions{Level: LevelInfo})
5959

60-
multi := MultiHandler(h1, h2)
60+
multi := NewMultiHandler(h1, h2)
6161

6262
if multi.Enabled(context.Background(), LevelDebug) {
6363
t.Error("Enabled should be false for DEBUG level, but got true")
@@ -69,7 +69,7 @@ func TestMultiHandler(t *testing.T) {
6969
h1 := NewTextHandler(&buf1, nil)
7070
h2 := NewJSONHandler(&buf2, nil)
7171

72-
multi := MultiHandler(h1, h2).WithAttrs([]Attr{String("request_id", "123")})
72+
multi := NewMultiHandler(h1, h2).WithAttrs([]Attr{String("request_id", "123")})
7373
logger := New(multi)
7474

7575
logger.Info("request processed")
@@ -83,7 +83,7 @@ func TestMultiHandler(t *testing.T) {
8383
h1 := NewTextHandler(&buf1, &HandlerOptions{AddSource: false})
8484
h2 := NewJSONHandler(&buf2, &HandlerOptions{AddSource: false})
8585

86-
multi := MultiHandler(h1, h2).WithGroup("req")
86+
multi := NewMultiHandler(h1, h2).WithGroup("req")
8787
logger := New(multi)
8888

8989
logger.Info("user login", "user_id", 42)
@@ -99,7 +99,7 @@ func TestMultiHandler(t *testing.T) {
9999
h1 := NewTextHandler(&buf1, nil)
100100
h2 := &mockFailingHandler{Handler: NewJSONHandler(&buf2, nil), err: errFail}
101101

102-
multi := MultiHandler(h2, h1)
102+
multi := NewMultiHandler(h2, h1)
103103

104104
err := multi.Handle(context.Background(), NewRecord(time.Now(), LevelInfo, "test message", 0))
105105
if !errors.Is(err, errFail) {
@@ -111,7 +111,7 @@ func TestMultiHandler(t *testing.T) {
111111
})
112112

113113
t.Run("Handle with no handlers", func(t *testing.T) {
114-
multi := MultiHandler()
114+
multi := NewMultiHandler()
115115
logger := New(multi)
116116

117117
logger.Info("nothing")
@@ -123,12 +123,12 @@ func TestMultiHandler(t *testing.T) {
123123
})
124124
}
125125

126-
// Test that MultiHandler copies the input slice and is insulated from future modification.
127-
func TestMultiHandlerCopy(t *testing.T) {
126+
// Test that NewMultiHandler copies the input slice and is insulated from future modification.
127+
func TestNewMultiHandlerCopy(t *testing.T) {
128128
var buf1 bytes.Buffer
129129
h1 := NewTextHandler(&buf1, nil)
130130
slice := []Handler{h1}
131-
multi := MultiHandler(slice...)
131+
multi := NewMultiHandler(slice...)
132132
slice[0] = nil
133133

134134
err := multi.Handle(context.Background(), NewRecord(time.Now(), LevelInfo, "test message", 0))

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /