GORM logging level is now based on the AppMode configuration

This commit is contained in:
Mael G. 2020-12-22 21:36:41 -04:00
parent 0b6b6d977c
commit deb28bbff9

View file

@ -9,6 +9,7 @@ import (
"gorm.io/driver/mysql"
"gorm.io/driver/postgres"
"gorm.io/gorm"
"gorm.io/gorm/logger"
)
//DB SQL database as global var
@ -19,16 +20,29 @@ var db *gorm.DB
func SQLDatabase(conf *Conf) {
logrus.WithFields(logrus.Fields{"database": conf.Database.Db, "driver": conf.Database.Type}).Infof("SQL : Connection to DB")
//Connect to the Database
var err error
var gormLogLevel logger.LogLevel
//Set GORM log level based on conf AppMode
if conf.AppMode != "production" {
gormLogLevel = logger.Info
} else {
gormLogLevel = logger.Silent
}
if conf.Database.Type == "postgresql" {
dsn := fmt.Sprintf("user=%s password=%s host=%s port=%s database=%s sslmode=disable", conf.Database.Username, conf.Database.Password, conf.Database.IP, conf.Database.Port, conf.Database.Db)
db, err = gorm.Open(postgres.Open(dsn), &gorm.Config{})
db, err = gorm.Open(postgres.Open(dsn), &gorm.Config{
Logger: logger.Default.LogMode(gormLogLevel),
})
} else {
dsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=True&loc=Local", conf.Database.Username, conf.Database.Password, conf.Database.IP, conf.Database.Port, conf.Database.Db)
db, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
db, err = gorm.Open(mysql.Open(dsn), &gorm.Config{
Logger: logger.Default.LogMode(gormLogLevel),
})
}
CheckErr(err)