From 4e728e5121140a61c5947b82740a68aaa96ebbe8 Mon Sep 17 00:00:00 2001 From: Andy Pan Date: Fri, 4 Aug 2023 06:47:56 +0800 Subject: [PATCH] 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 Reviewed-by: Michael Knyszek TryBot-Result: Gopher Robot Reviewed-by: Jonathan Amsterdam --- src/log/slog/logger_test.go | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/log/slog/logger_test.go b/src/log/slog/logger_test.go index 559b9d66b4..7d0243e415 100644 --- a/src/log/slog/logger_test.go +++ b/src/log/slog/logger_test.go @@ -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