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"
pull/2184/head
Timothy Messier 4 years ago
parent 07a9789abc
commit f3c90df300
No known key found for this signature in database
GPG Key ID: EFD2F184F7600572

@ -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)

Loading…
Cancel
Save