Silence gorm default logger

Disable the default logger for gorm when debug is not set on the sql
store backend. By default gorm will print error level log messages,
which includes queries which find no results, which is not really an
error.

These messages will show up if debug is enabled on the sql store
backend.

Signed-off-by: Gabriel Adrian Samfira <gsamfira@cloudbasesolutions.com>
This commit is contained in:
Gabriel Adrian Samfira 2022-09-06 21:35:01 +03:00
parent c0df364bb0
commit 99856af4de
No known key found for this signature in database
GPG key ID: 7D073DCC2C074CB5

View file

@ -21,6 +21,7 @@ import (
"gorm.io/driver/mysql"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
"gorm.io/gorm/logger"
"garm/config"
"garm/database/common"
@ -32,11 +33,17 @@ func newDBConn(dbCfg config.Database) (conn *gorm.DB, err error) {
if err != nil {
return nil, errors.Wrap(err, "getting DB URI string")
}
gormConfig := &gorm.Config{}
if !dbCfg.Debug {
gormConfig.Logger = logger.Default.LogMode(logger.Silent)
}
switch dbType {
case config.MySQLBackend:
conn, err = gorm.Open(mysql.Open(connURI), &gorm.Config{})
conn, err = gorm.Open(mysql.Open(connURI), gormConfig)
case config.SQLiteBackend:
conn, err = gorm.Open(sqlite.Open(connURI), &gorm.Config{})
conn, err = gorm.Open(sqlite.Open(connURI), gormConfig)
}
if err != nil {
return nil, errors.Wrap(err, "connecting to database")