mirror of
https://github.com/golang/go.git
synced 2025-05-05 15:43:04 +00:00
log/slog: make examples playable
Fixes #69246 Change-Id: I8e59132980404ee58ba2ca8718dd9f68404fdf8d GitHub-Last-Rev: f59d3fad2002cda69a24789f2fdd4e9753cede9c GitHub-Pull-Request: golang/go#69249 Reviewed-on: https://go-review.googlesource.com/c/go/+/610535 Reviewed-by: Sean Liao <sean@liao.dev> Auto-Submit: Sean Liao <sean@liao.dev> Reviewed-by: Dmitri Shuralyov <dmitshur@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Jonathan Amsterdam <jba@google.com>
This commit is contained in:
parent
3046b49991
commit
20a924fe87
@ -6,15 +6,20 @@ package slog_test
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"log/slog/internal/slogtest"
|
|
||||||
"os"
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Example_discardHandler() {
|
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.
|
// A slog.TextHandler can output log messages.
|
||||||
logger1 := slog.New(slog.NewTextHandler(
|
logger1 := slog.New(slog.NewTextHandler(
|
||||||
os.Stdout,
|
os.Stdout,
|
||||||
&slog.HandlerOptions{ReplaceAttr: slogtest.RemoveTime},
|
&slog.HandlerOptions{ReplaceAttr: removeTime},
|
||||||
))
|
))
|
||||||
logger1.Info("message 1")
|
logger1.Info("message 1")
|
||||||
|
|
||||||
|
@ -7,7 +7,6 @@ package slog_test
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"log/slog/internal/slogtest"
|
|
||||||
"os"
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -63,7 +62,13 @@ func (h *LevelHandler) Handler() slog.Handler {
|
|||||||
// Another typical use would be to decrease the log level (to LevelDebug, say)
|
// 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.
|
// during a part of the program that was suspected of containing a bug.
|
||||||
func ExampleHandler_levelHandler() {
|
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 := slog.New(NewLevelHandler(slog.LevelWarn, th))
|
||||||
logger.Info("not printed")
|
logger.Info("not printed")
|
||||||
logger.Warn("printed")
|
logger.Warn("printed")
|
||||||
|
@ -7,7 +7,6 @@ package slog_test
|
|||||||
import (
|
import (
|
||||||
"log"
|
"log"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"log/slog/internal/slogtest"
|
|
||||||
"os"
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -49,7 +48,13 @@ func ExampleSetLogLoggerLevel_slog() {
|
|||||||
defer slog.SetLogLoggerLevel(currentLogLevel) // revert changes after the example
|
defer slog.SetLogLoggerLevel(currentLogLevel) // revert changes after the example
|
||||||
|
|
||||||
defer slog.SetDefault(slog.Default()) // 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
|
log.Print("error") // level=ERROR msg=error
|
||||||
|
|
||||||
|
@ -6,7 +6,6 @@ package slog_test
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"log/slog/internal/slogtest"
|
|
||||||
"os"
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -23,7 +22,13 @@ func (Token) LogValue() slog.Value {
|
|||||||
// with an alternative representation to avoid revealing secrets.
|
// with an alternative representation to avoid revealing secrets.
|
||||||
func ExampleLogValuer_secret() {
|
func ExampleLogValuer_secret() {
|
||||||
t := Token("shhhh!")
|
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)
|
logger.Info("permission granted", "user", "Perry", "token", t)
|
||||||
|
|
||||||
// Output:
|
// 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…
x
Reference in New Issue
Block a user