From f3c90df3007a5ca7be758a059ce8283791dd4e78 Mon Sep 17 00:00:00 2001 From: Timothy Messier Date: Thu, 7 Apr 2022 10:53:37 -0400 Subject: [PATCH] chore(db): Silence db logs by default for tests This removes a lot of log clutter from normal test execution with `go test -v`. There are a number of test that expect no results from the database or other error cases from the database. The previous default log level of dbw.Error would cause these cases to generate logs in the success case, creating noise in the test output. The new default for tests is dbw.Silent. This can be overriden in two ways. A test can be updated to pass in the db.WithTestLogLevel option, which allows for targeting specific tests to include additional output. This may make sense for tests that normally should not cause errors, but would report an error if it fails. Alternatively a new environment variable can be set, DEFAULT_TEST_LOG_LEVEL. This allows for adjusting the log level without changing any test file. The environment variable should be set to the string value of the log level, and is treated as case insensitive, ie: export DEFAULT_TEST_LOG_LEVEL="error" --- internal/db/testing.go | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/internal/db/testing.go b/internal/db/testing.go index b97c58ff36..4b058a10db 100644 --- a/internal/db/testing.go +++ b/internal/db/testing.go @@ -5,6 +5,7 @@ import ( "crypto/rand" "encoding/base64" "fmt" + "os" "strings" "testing" "time" @@ -65,7 +66,20 @@ func TestSetup(t testing.TB, dialect string, opt ...TestOption) (*DB, string) { case opts.withLogLevel != DefaultTestLogLevel: db.wrapped.LogLevel(dbw.LogLevel(opts.withLogLevel)) default: - db.wrapped.LogLevel(dbw.Error) + var defaultLevel dbw.LogLevel + switch strings.ToLower(os.Getenv("DEFAULT_TEST_LOG_LEVEL")) { + case "silent": + defaultLevel = dbw.Silent + case "error": + defaultLevel = dbw.Error + case "warn": + defaultLevel = dbw.Warn + case "info": + defaultLevel = dbw.Info + default: + defaultLevel = dbw.Silent + } + db.wrapped.LogLevel(defaultLevel) } t.Cleanup(func() { sqlDB, err := db.SqlDB(ctx)