1
0
mirror of https://github.com/golang/go synced 2024-11-18 01:54:45 -07:00

log/slog: restore the original log setting before test exits

Change-Id: Ib3daffb8a4cc018d62ed6e5741355b1c1a206034
Reviewed-on: https://go-review.googlesource.com/c/go/+/515775
Run-TryBot: Andy Pan <panjf2000@gmail.com>
Reviewed-by: Michael Knyszek <mknyszek@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Jonathan Amsterdam <jba@google.com>
This commit is contained in:
Andy Pan 2023-08-04 06:47:56 +08:00 committed by Jonathan Amsterdam
parent 0c2abb3233
commit 4e728e5121

View File

@ -12,7 +12,6 @@ import (
"io" "io"
"log" "log"
loginternal "log/internal" loginternal "log/internal"
"os"
"path/filepath" "path/filepath"
"regexp" "regexp"
"runtime" "runtime"
@ -73,9 +72,13 @@ func TestConnections(t *testing.T) {
// tests might change the default logger using SetDefault. Also ensure we // tests might change the default logger using SetDefault. Also ensure we
// restore the default logger at the end of the test. // restore the default logger at the end of the test.
currentLogger := Default() currentLogger := Default()
currentLogWriter := log.Writer()
currentLogFlags := log.Flags()
SetDefault(New(newDefaultHandler(loginternal.DefaultOutput))) SetDefault(New(newDefaultHandler(loginternal.DefaultOutput)))
t.Cleanup(func() { t.Cleanup(func() {
SetDefault(currentLogger) SetDefault(currentLogger)
log.SetOutput(currentLogWriter)
log.SetFlags(currentLogFlags)
}) })
// The default slog.Logger's handler uses the log package's default output. // The default slog.Logger's handler uses the log package's default output.
@ -598,10 +601,12 @@ func TestPanics(t *testing.T) {
// tests might change the default logger using SetDefault. Also ensure we // tests might change the default logger using SetDefault. Also ensure we
// restore the default logger at the end of the test. // restore the default logger at the end of the test.
currentLogger := Default() currentLogger := Default()
currentLogWriter := log.Writer()
currentLogFlags := log.Flags()
t.Cleanup(func() { t.Cleanup(func() {
SetDefault(currentLogger) SetDefault(currentLogger)
log.SetOutput(os.Stderr) log.SetOutput(currentLogWriter)
log.SetFlags(log.LstdFlags) log.SetFlags(currentLogFlags)
}) })
var logBuf bytes.Buffer var logBuf bytes.Buffer