Skip to content
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

log/slog: factoring out code to make the examples playable #69249

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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_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
12 changes: 0 additions & 12 deletions src/log/slog/internal/slogtest/slogtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,3 @@

// Package slogtest contains support functions for testing slog.
package slogtest

import "log/slog"

// RemoveTime removes the top-level time attribute.
// It is intended to be used as a ReplaceAttr function,
// to make example output deterministic.
func RemoveTime(groups []string, a slog.Attr) slog.Attr {
if a.Key == slog.TimeKey && len(groups) == 0 {
return slog.Attr{}
}
return a
}