Skip to content

Commit

Permalink
Add util to initialize a gorm logger impl from config (flyteorg#128)
Browse files Browse the repository at this point in the history
* Add GetGormLogger

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

* Add GetGormLogger

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

* goimports

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

* no op

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

* try a thing

Signed-off-by: Katrina Rogan <[email protected]>
  • Loading branch information
katrogan authored Apr 11, 2022
1 parent 8405650 commit d05d262
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
2 changes: 1 addition & 1 deletion flytestdlib/.github/workflows/pull_request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
50 changes: 50 additions & 0 deletions flytestdlib/database/gorm.go
Original file line number Diff line number Diff line change
@@ -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,
})
}

0 comments on commit d05d262

Please sign in to comment.