mirror of
https://github.com/golang/go
synced 2024-09-29 12:04:28 -06:00
log/slog: don't call SetDefault in examples
Replace the default Logger in some examples with a locally constructed Logger. Calling SetDefault changes global state that could affect other tests. Although we could use a defer to restore the state, that clutters the example and would not work if tests were run concurrently. Change-Id: Ib2595c57f8e6c3e0b39b982f682ba287c2ae249d Reviewed-on: https://go-review.googlesource.com/c/go/+/482475 Run-TryBot: Jonathan Amsterdam <jba@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Alan Donovan <adonovan@google.com>
This commit is contained in:
parent
89567a35c1
commit
6116a47800
@ -17,9 +17,7 @@ func ExampleGroup() {
|
|||||||
// ...
|
// ...
|
||||||
|
|
||||||
logger := slog.New(slog.HandlerOptions{ReplaceAttr: slogtest.RemoveTime}.NewTextHandler(os.Stdout))
|
logger := slog.New(slog.HandlerOptions{ReplaceAttr: slogtest.RemoveTime}.NewTextHandler(os.Stdout))
|
||||||
slog.SetDefault(logger)
|
logger.Info("finished",
|
||||||
|
|
||||||
slog.Info("finished",
|
|
||||||
slog.Group("req",
|
slog.Group("req",
|
||||||
slog.String("method", r.Method),
|
slog.String("method", r.Method),
|
||||||
slog.String("url", r.URL.String())),
|
slog.String("url", r.URL.String())),
|
||||||
|
@ -16,20 +16,17 @@ import (
|
|||||||
|
|
||||||
// Infof is an example of a user-defined logging function that wraps slog.
|
// Infof is an example of a user-defined logging function that wraps slog.
|
||||||
// The log record contains the source position of the caller of Infof.
|
// The log record contains the source position of the caller of Infof.
|
||||||
func Infof(format string, args ...any) {
|
func Infof(logger *slog.Logger, format string, args ...any) {
|
||||||
l := slog.Default()
|
if !logger.Enabled(context.Background(), slog.LevelInfo) {
|
||||||
if !l.Enabled(context.Background(), slog.LevelInfo) {
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
var pcs [1]uintptr
|
var pcs [1]uintptr
|
||||||
runtime.Callers(2, pcs[:]) // skip [Callers, Infof]
|
runtime.Callers(2, pcs[:]) // skip [Callers, Infof]
|
||||||
r := slog.NewRecord(time.Now(), slog.LevelInfo, fmt.Sprintf(format, args...), pcs[0])
|
r := slog.NewRecord(time.Now(), slog.LevelInfo, fmt.Sprintf(format, args...), pcs[0])
|
||||||
_ = l.Handler().Handle(context.Background(), r)
|
_ = logger.Handler().Handle(context.Background(), r)
|
||||||
}
|
}
|
||||||
|
|
||||||
func Example_wrapping() {
|
func Example_wrapping() {
|
||||||
defer func(l *slog.Logger) { slog.SetDefault(l) }(slog.Default())
|
|
||||||
|
|
||||||
replace := func(groups []string, a slog.Attr) slog.Attr {
|
replace := func(groups []string, a slog.Attr) slog.Attr {
|
||||||
// Remove time.
|
// Remove time.
|
||||||
if a.Key == slog.TimeKey && len(groups) == 0 {
|
if a.Key == slog.TimeKey && len(groups) == 0 {
|
||||||
@ -42,9 +39,8 @@ func Example_wrapping() {
|
|||||||
return a
|
return a
|
||||||
}
|
}
|
||||||
logger := slog.New(slog.HandlerOptions{AddSource: true, ReplaceAttr: replace}.NewTextHandler(os.Stdout))
|
logger := slog.New(slog.HandlerOptions{AddSource: true, ReplaceAttr: replace}.NewTextHandler(os.Stdout))
|
||||||
slog.SetDefault(logger)
|
Infof(logger, "message, %s", "formatted")
|
||||||
Infof("message, %s", "formatted")
|
|
||||||
|
|
||||||
// Output:
|
// Output:
|
||||||
// level=INFO source=example_wrap_test.go:46 msg="message, formatted"
|
// level=INFO source=example_wrap_test.go:42 msg="message, formatted"
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user