Skip to content

Commit

Permalink
Update gorm logger initialization (flyteorg#398)
Browse files Browse the repository at this point in the history
* update

Signed-off-by: Katrina Rogan <[email protected]>

* lint

Signed-off-by: Katrina Rogan <[email protected]>
  • Loading branch information
katrogan authored Apr 12, 2022
1 parent e37c8e2 commit 3f21a34
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 44 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ require (
github.com/flyteorg/flyteidl v0.24.18
github.com/flyteorg/flyteplugins v0.10.16
github.com/flyteorg/flytepropeller v0.16.36
github.com/flyteorg/flytestdlib v0.4.21
github.com/flyteorg/flytestdlib v0.4.23
github.com/flyteorg/stow v0.3.3
github.com/ghodss/yaml v1.0.0
github.com/go-gormigrate/gormigrate/v2 v2.0.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -377,8 +377,8 @@ github.com/flyteorg/flytepropeller v0.16.36 h1:5uE8JsutrPVyLVrRJ8BgvhZUOmTBFkEkn
github.com/flyteorg/flytepropeller v0.16.36/go.mod h1:DGCjQSRp8VYOBH56aQyAZfNf1Vgh+GNpwQL7uhottYM=
github.com/flyteorg/flytestdlib v0.3.13/go.mod h1:Tz8JCECAbX6VWGwFT6cmEQ+RJpZ/6L9pswu3fzWs220=
github.com/flyteorg/flytestdlib v0.4.13/go.mod h1:fv1ar34LJLMTaf0tbfetisLykUlARi7rP+NQTUn6QQs=
github.com/flyteorg/flytestdlib v0.4.21 h1:IiIYzxwSehtFXH68EJC2J/6n2duemOZkn5LRVSw4YiE=
github.com/flyteorg/flytestdlib v0.4.21/go.mod h1:QSVN5wIM1lM9d60eAEbX7NwweQXW96t5x4jbyftn89c=
github.com/flyteorg/flytestdlib v0.4.23 h1:REDpbqzhvuT4biIXPHkWVHpEqs3OMVninB9YadhIPK0=
github.com/flyteorg/flytestdlib v0.4.23/go.mod h1:QSVN5wIM1lM9d60eAEbX7NwweQXW96t5x4jbyftn89c=
github.com/flyteorg/stow v0.3.3 h1:tzeNl8mSZFL3oJDi0ACZj6FAineQAF4qyEp6bXtIdQY=
github.com/flyteorg/stow v0.3.3/go.mod h1:HBld7ud0i4khMHwJjkO8v+NSP7ddKa/ruhf4I8fliaA=
github.com/fogleman/gg v1.2.1-0.20190220221249-0403632d5b90/go.mod h1:R/bRT+9gY/C5z7JzPU0zXsXHKM4/ayA+zqcVNZzPa1k=
Expand Down
44 changes: 3 additions & 41 deletions pkg/repositories/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import (
"context"
"fmt"
"io/ioutil"
"log"
"os"
"reflect"
"strings"
"time"

"github.com/flyteorg/flytestdlib/database"

repoErrors "github.com/flyteorg/flyteadmin/pkg/repositories/errors"
"gorm.io/driver/sqlite"
Expand All @@ -18,49 +18,11 @@ import (
"github.com/jackc/pgconn"
"gorm.io/driver/postgres"
"gorm.io/gorm"
gormLogger "gorm.io/gorm/logger"
)

const pqInvalidDBCode = "3D000"
const defaultDB = "postgres"

// getGormLogLevel converts between the flytestdlib configured log level to the equivalent gorm log level.
func getGormLogger(ctx context.Context, logConfig *logger.Config) gormLogger.Interface {
logConfigLevel := logger.ErrorLevel
if logConfig != nil {
logConfigLevel = logConfig.Level
} else {
logger.Debugf(ctx, "No log config block found, setting gorm db log level to: error")
}
var logLevel gormLogger.LogLevel
ignoreRecordNotFoundError := true
switch logConfigLevel {
case logger.PanicLevel:
fallthrough
case logger.FatalLevel:
fallthrough
case logger.ErrorLevel:
logLevel = gormLogger.Error
case logger.WarnLevel:
logLevel = gormLogger.Warn
case logger.InfoLevel:
fallthrough
case logger.DebugLevel:
logLevel = gormLogger.Info
ignoreRecordNotFoundError = false
default:
logLevel = gormLogger.Silent
}
// Copied from gormLogger.Default initialization. The gormLogger interface only allows modifying the LogLevel
// and not IgnoreRecordNotFoundError.
return gormLogger.New(log.New(os.Stdout, "\r\n", log.LstdFlags), gormLogger.Config{
SlowThreshold: 200 * time.Millisecond,
LogLevel: logLevel,
IgnoreRecordNotFoundError: ignoreRecordNotFoundError,
Colorful: true,
})
}

// Resolves a password value from either a user-provided inline value or a filepath whose contents contain a password.
func resolvePassword(ctx context.Context, passwordVal, passwordPath string) string {
password := passwordVal
Expand Down Expand Up @@ -101,7 +63,7 @@ func GetDB(ctx context.Context, dbConfig *runtimeInterfaces.DbConfig, logConfig
panic("Cannot initialize database repository from empty db config")
}
gormConfig := &gorm.Config{
Logger: getGormLogger(ctx, logConfig),
Logger: database.GetGormLogger(ctx, logConfig),
DisableForeignKeyConstraintWhenMigrating: !dbConfig.EnableForeignKeyConstraintWhenMigrating,
}

Expand Down

0 comments on commit 3f21a34

Please sign in to comment.