diff --git a/flytestdlib/.github/workflows/pull_request.yml b/flytestdlib/.github/workflows/pull_request.yml index 9c8f7f566f..7f83394a00 100644 --- a/flytestdlib/.github/workflows/pull_request.yml +++ b/flytestdlib/.github/workflows/pull_request.yml @@ -17,7 +17,7 @@ jobs: - name: Unit Tests run: make mod_download && make test_unit_codecov - name: Push CodeCov - uses: codecov/codecov-action@v1 + uses: codecov/codecov-action@v1.5.2 with: file: coverage.txt flags: unittests diff --git a/flytestdlib/database/gorm.go b/flytestdlib/database/gorm.go new file mode 100644 index 0000000000..db8f7a32a3 --- /dev/null +++ b/flytestdlib/database/gorm.go @@ -0,0 +1,50 @@ +package database + +import ( + "context" + "log" + "os" + "time" + + "github.com/flyteorg/flytestdlib/logger" + + gormLogger "gorm.io/gorm/logger" +) + +// GetGormLogger converts between the flytestdlib configured log level to the equivalent gorm log level and outputs +// a gorm/logger implementation accordingly configured. +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, + }) +}