GORM logging level is now based on the AppMode configuration
This commit is contained in:
parent
0b6b6d977c
commit
deb28bbff9
1 changed files with 16 additions and 2 deletions
18
utils/sql.go
18
utils/sql.go
|
@ -9,6 +9,7 @@ import (
|
||||||
"gorm.io/driver/mysql"
|
"gorm.io/driver/mysql"
|
||||||
"gorm.io/driver/postgres"
|
"gorm.io/driver/postgres"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
|
"gorm.io/gorm/logger"
|
||||||
)
|
)
|
||||||
|
|
||||||
//DB SQL database as global var
|
//DB SQL database as global var
|
||||||
|
@ -19,16 +20,29 @@ var db *gorm.DB
|
||||||
func SQLDatabase(conf *Conf) {
|
func SQLDatabase(conf *Conf) {
|
||||||
logrus.WithFields(logrus.Fields{"database": conf.Database.Db, "driver": conf.Database.Type}).Infof("SQL : Connection to DB")
|
logrus.WithFields(logrus.Fields{"database": conf.Database.Db, "driver": conf.Database.Type}).Infof("SQL : Connection to DB")
|
||||||
//Connect to the Database
|
//Connect to the Database
|
||||||
|
|
||||||
var err error
|
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" {
|
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)
|
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 {
|
} 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)
|
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)
|
CheckErr(err)
|
||||||
|
|
Loading…
Reference in a new issue