DEV Community

badruti94
badruti94

Posted on

Membuat logger pada gorm

#go
package db

import (
    "log"
    "os"

    "gorm.io/driver/postgres"
    "gorm.io/gorm"
    "gorm.io/gorm/logger"
)

func Connect() *gorm.DB {
    os.Getenv("CLOUDINARY_NAME")
    dsn := os.Getenv("DB_URL")
    db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{
        Logger: logger.New(
            log.New(log.Writer(), "\r\n", log.LstdFlags),
            logger.Config{
                SlowThreshold:             200,
                Colorful:                  true,
                IgnoreRecordNotFoundError: false,
                LogLevel:                  logger.Info,
            },
        ),
    })

    if err != nil {
        panic(err)
    }

    return db
}

Enter fullscreen mode Exit fullscreen mode

Top comments (0)