Skip to content

Commit

Permalink
slog: factoring out code to make the examples playable and remove unu…
Browse files Browse the repository at this point in the history
…sed test method
  • Loading branch information
haoxins committed Sep 18, 2024
1 parent b28b263 commit 68d9098
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 18 deletions.
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
}

0 comments on commit 68d9098

Please sign in to comment.