Skip to content

Commit a9dfb77

Browse files
committed
slog: factoring out code to make the examples playable and remove unused test method
1 parent 1b5ae45 commit a9dfb77

File tree

4 files changed

+21
-18
lines changed

4 files changed

+21
-18
lines changed

src/log/slog/example_level_handler_test.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ package slog_test
77
import (
88
"context"
99
"log/slog"
10-
"log/slog/internal/slogtest"
1110
"os"
1211
)
1312

@@ -63,7 +62,13 @@ func (h *LevelHandler) Handler() slog.Handler {
6362
// Another typical use would be to decrease the log level (to LevelDebug, say)
6463
// during a part of the program that was suspected of containing a bug.
6564
func ExampleHandler_levelHandler() {
66-
th := slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{ReplaceAttr: slogtest.RemoveTime})
65+
removeTime := func(groups []string, a slog.Attr) slog.Attr {
66+
if a.Key == slog.TimeKey && len(groups) == 0 {
67+
return slog.Attr{}
68+
}
69+
return a
70+
}
71+
th := slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{ReplaceAttr: removeTime})
6772
logger := slog.New(NewLevelHandler(slog.LevelWarn, th))
6873
logger.Info("not printed")
6974
logger.Warn("printed")

src/log/slog/example_log_level_test.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ package slog_test
77
import (
88
"log"
99
"log/slog"
10-
"log/slog/internal/slogtest"
1110
"os"
1211
)
1312

@@ -49,7 +48,13 @@ func ExampleSetLogLoggerLevel_slog() {
4948
defer slog.SetLogLoggerLevel(currentLogLevel) // revert changes after the example
5049

5150
defer slog.SetDefault(slog.Default()) // revert changes after the example
52-
slog.SetDefault(slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{ReplaceAttr: slogtest.RemoveTime})))
51+
removeTime := func(groups []string, a slog.Attr) slog.Attr {
52+
if a.Key == slog.TimeKey && len(groups) == 0 {
53+
return slog.Attr{}
54+
}
55+
return a
56+
}
57+
slog.SetDefault(slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{ReplaceAttr: removeTime})))
5358

5459
log.Print("error") // level=ERROR msg=error
5560

src/log/slog/example_logvaluer_secret_test.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ package slog_test
66

77
import (
88
"log/slog"
9-
"log/slog/internal/slogtest"
109
"os"
1110
)
1211

@@ -23,7 +22,13 @@ func (Token) LogValue() slog.Value {
2322
// with an alternative representation to avoid revealing secrets.
2423
func ExampleLogValuer_secret() {
2524
t := Token("shhhh!")
26-
logger := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{ReplaceAttr: slogtest.RemoveTime}))
25+
removeTime := func(groups []string, a slog.Attr) slog.Attr {
26+
if a.Key == slog.TimeKey && len(groups) == 0 {
27+
return slog.Attr{}
28+
}
29+
return a
30+
}
31+
logger := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{ReplaceAttr: removeTime}))
2732
logger.Info("permission granted", "user", "Perry", "token", t)
2833

2934
// Output:

src/log/slog/internal/slogtest/slogtest.go

-12
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,3 @@
44

55
// Package slogtest contains support functions for testing slog.
66
package slogtest
7-
8-
import "log/slog"
9-
10-
// RemoveTime removes the top-level time attribute.
11-
// It is intended to be used as a ReplaceAttr function,
12-
// to make example output deterministic.
13-
func RemoveTime(groups []string, a slog.Attr) slog.Attr {
14-
if a.Key == slog.TimeKey && len(groups) == 0 {
15-
return slog.Attr{}
16-
}
17-
return a
18-
}

0 commit comments

Comments
 (0)