Skip to content

log/slog: make examples playable #69249

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/log/slog/example_discard_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,20 @@ package slog_test

import (
"log/slog"
"log/slog/internal/slogtest"
"os"
)

func Example_discardHandler() {
removeTime := func(groups []string, a slog.Attr) slog.Attr {
if a.Key == slog.TimeKey && len(groups) == 0 {
return slog.Attr{}
}
return a
}
// A slog.TextHandler can output log messages.
logger1 := slog.New(slog.NewTextHandler(
os.Stdout,
&slog.HandlerOptions{ReplaceAttr: slogtest.RemoveTime},
&slog.HandlerOptions{ReplaceAttr: removeTime},
))
logger1.Info("message 1")

Expand Down
9 changes: 7 additions & 2 deletions src/log/slog/example_level_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ package slog_test
import (
"context"
"log/slog"
"log/slog/internal/slogtest"
"os"
)

Expand Down Expand Up @@ -63,7 +62,13 @@ func (h *LevelHandler) Handler() slog.Handler {
// Another typical use would be to decrease the log level (to LevelDebug, say)
// during a part of the program that was suspected of containing a bug.
func ExampleHandler_levelHandler() {
th := slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{ReplaceAttr: slogtest.RemoveTime})
removeTime := func(groups []string, a slog.Attr) slog.Attr {
if a.Key == slog.TimeKey && len(groups) == 0 {
return slog.Attr{}
}
return a
}
th := slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{ReplaceAttr: removeTime})
logger := slog.New(NewLevelHandler(slog.LevelWarn, th))
logger.Info("not printed")
logger.Warn("printed")
Expand Down
9 changes: 7 additions & 2 deletions src/log/slog/example_log_level_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ package slog_test
import (
"log"
"log/slog"
"log/slog/internal/slogtest"
"os"
)

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

defer slog.SetDefault(slog.Default()) // revert changes after the example
slog.SetDefault(slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{ReplaceAttr: slogtest.RemoveTime})))
removeTime := func(groups []string, a slog.Attr) slog.Attr {
if a.Key == slog.TimeKey && len(groups) == 0 {
return slog.Attr{}
}
return a
}
slog.SetDefault(slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{ReplaceAttr: removeTime})))

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

Expand Down
9 changes: 7 additions & 2 deletions src/log/slog/example_logvaluer_secret_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package slog_test

import (
"log/slog"
"log/slog/internal/slogtest"
"os"
)

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

// Output:
Expand Down
18 changes: 0 additions & 18 deletions src/log/slog/internal/slogtest/slogtest.go

This file was deleted.