Skip to content

Commit a3f4ac0

Browse files
committed
Fixes panic due to struct initiliaztion
This would panic due to not setting up default arguments such as the Formatter in the logger when using the FC_TEST_LOG_LEVEL. This fix instead uses log.New() to have the necessary bootstrapping occur and sets the necessary fields as needed. Signed-off-by: xibz <[email protected]>
1 parent 9f04cde commit a3f4ac0

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

fctesting/utils.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,18 +49,18 @@ func RequiresRoot(t testing.TB) {
4949

5050
func newLogger(t testing.TB) *log.Logger {
5151
str := os.Getenv(logLevelEnvName)
52+
l := log.New()
5253
if str == "" {
53-
return log.New()
54+
return l
5455
}
5556

5657
logLevel, err := log.ParseLevel(str)
5758
if err != nil {
5859
t.Fatalf("Failed to parse %q as Log Level: %v", str, err)
5960
}
60-
return &log.Logger{
61-
Out: os.Stdout,
62-
Level: logLevel,
63-
}
61+
62+
l.SetLevel(logLevel)
63+
return l
6464
}
6565

6666
// NewLogEntry creates log.Entry. The level is specified by "FC_TEST_LOG_LEVEL" environment variable

fctesting/utils_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package fctesting
2+
3+
import (
4+
"os"
5+
"testing"
6+
)
7+
8+
func TestLoggingPanic(t *testing.T) {
9+
defer func() {
10+
if r := recover(); r != nil {
11+
t.Error(r)
12+
}
13+
}()
14+
15+
os.Setenv("FC_TEST_LOG_LEVEL", "debug")
16+
l := NewLogEntry(t)
17+
l.Debug("TestLoggingPanic")
18+
}

0 commit comments

Comments
 (0)