forked from flyteorg/flyte
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add util to initialize a gorm logger impl from config (flyteorg#128)
* 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
Showing
2 changed files
with
51 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}) | ||
} |