From 512e5034b40f034bff97f7ba504bf000b5bcb885 Mon Sep 17 00:00:00 2001 From: apstndb <803393+apstndb@users.noreply.github.com> Date: Thu, 5 Dec 2024 21:43:10 +0900 Subject: [PATCH] chore: enable implicit default logger only in testing with -v (#2877) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Enable default logger only in testing * Fix default logger assigning --------- Co-authored-by: Manuel de la Peña --- logger.go | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/logger.go b/logger.go index fca5da5398..1a5ae5dcdb 100644 --- a/logger.go +++ b/logger.go @@ -11,17 +11,18 @@ import ( ) // Logger is the default log instance -var Logger Logging = log.New(os.Stderr, "", log.LstdFlags) +var Logger Logging = &noopLogger{} func init() { - for _, arg := range os.Args { - if strings.EqualFold(arg, "-test.v=true") || strings.EqualFold(arg, "-v") { - return + // Enable default logger in the testing with a verbose flag. + if testing.Testing() { + // Parse manually because testing.Verbose() panics unless flag.Parse() has done. + for _, arg := range os.Args { + if strings.EqualFold(arg, "-test.v=true") || strings.EqualFold(arg, "-v") { + Logger = log.New(os.Stderr, "", log.LstdFlags) + } } } - - // If we are not running in verbose mode, we configure a noop logger by default. - Logger = &noopLogger{} } // Validate our types implement the required interfaces.