Skip to content

Commit 9c40d1c

Browse files
committed
5
Change-Id: I8dc2a9e008e945e7338b3f1ab14216c99442f15e
1 parent 2b23315 commit 9c40d1c

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright 2022 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package slog_test
6+
7+
import (
8+
"bytes"
9+
"log/slog"
10+
"os"
11+
)
12+
13+
func ExampleMultiHandler() {
14+
removeTime := func(groups []string, a slog.Attr) slog.Attr {
15+
if a.Key == slog.TimeKey && len(groups) == 0 {
16+
return slog.Attr{}
17+
}
18+
return a
19+
}
20+
21+
var textBuf, jsonBuf bytes.Buffer
22+
textHandler := slog.NewTextHandler(&textBuf, &slog.HandlerOptions{ReplaceAttr: removeTime})
23+
jsonHandler := slog.NewJSONHandler(&jsonBuf, &slog.HandlerOptions{ReplaceAttr: removeTime})
24+
25+
multiHandler := slog.MultiHandler(textHandler, jsonHandler)
26+
logger := slog.New(multiHandler)
27+
28+
logger.Info("login",
29+
slog.String("name", "whoami"),
30+
slog.Int("id", 42),
31+
)
32+
33+
os.Stdout.WriteString(textBuf.String())
34+
os.Stdout.WriteString(jsonBuf.String())
35+
36+
// Output:
37+
// level=INFO msg=login name=whoami id=42
38+
// {"level":"INFO","msg":"login","name":"whoami","id":42}
39+
}

0 commit comments

Comments
 (0)