mirror of
https://github.com/golang/go
synced 2024-11-24 00:10:04 -07:00
slog: factoring out code to make the examples playable and remove unused test method
This commit is contained in:
parent
eb6f2c24cd
commit
95c0aa1ea4
@ -7,7 +7,6 @@ package slog_test
|
||||
import (
|
||||
"context"
|
||||
"log/slog"
|
||||
"log/slog/internal/slogtest"
|
||||
"os"
|
||||
)
|
||||
|
||||
@ -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")
|
||||
|
@ -7,7 +7,6 @@ package slog_test
|
||||
import (
|
||||
"log"
|
||||
"log/slog"
|
||||
"log/slog/internal/slogtest"
|
||||
"os"
|
||||
)
|
||||
|
||||
@ -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
|
||||
|
||||
|
@ -6,7 +6,6 @@ package slog_test
|
||||
|
||||
import (
|
||||
"log/slog"
|
||||
"log/slog/internal/slogtest"
|
||||
"os"
|
||||
)
|
||||
|
||||
@ -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:
|
||||
|
@ -1,18 +0,0 @@
|
||||
// Copyright 2022 The Go Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
// 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
|
||||
}
|
Loading…
Reference in New Issue
Block a user