1
0
mirror of https://github.com/golang/go synced 2024-11-26 18:16:48 -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"
"log"
loginternal "log/internal"
"os"
"path/filepath"
"regexp"
"runtime"
@ -73,9 +72,13 @@ func TestConnections(t *testing.T) {
// tests might change the default logger using SetDefault. Also ensure we
// restore the default logger at the end of the test.
currentLogger := Default()
currentLogWriter := log.Writer()
currentLogFlags := log.Flags()
SetDefault(New(newDefaultHandler(loginternal.DefaultOutput)))
t.Cleanup(func() {
SetDefault(currentLogger)
log.SetOutput(currentLogWriter)
log.SetFlags(currentLogFlags)
})
// 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
// restore the default logger at the end of the test.
currentLogger := Default()
currentLogWriter := log.Writer()
currentLogFlags := log.Flags()
t.Cleanup(func() {
SetDefault(currentLogger)
log.SetOutput(os.Stderr)
log.SetFlags(log.LstdFlags)
log.SetOutput(currentLogWriter)
log.SetFlags(currentLogFlags)
})
var logBuf bytes.Buffer