thomas-tacquet / gormv2-logrus

Easily connect Gorm V2 and logrus with customizable options
MIT License
9 stars 3 forks source link

logging not working #9

Open sysmat opened 1 year ago

sysmat commented 1 year ago
require (
    github.com/coreos/go-systemd/v22 v22.5.0
    github.com/gin-gonic/gin v1.9.1
    github.com/sirupsen/logrus v1.9.3
    github.com/thomas-tacquet/gormv2-logrus v1.2.2
    gopkg.in/natefinch/lumberjack.v2 v2.2.1
    gorm.io/driver/mysql v1.5.1
    gorm.io/gorm v1.25.1
)
gormLogger := gormv2logrus.NewGormlog(
        gormv2logrus.WithLogrus(l),
        gormv2logrus.WithGormOptions(
            gormv2logrus.GormOptions{
                SlowThreshold: slowThresholdDuration,
                LogLevel:      logger.Info,
                LogLatency:    true,
            },
        ),
    )

gormr, err := gorm.Open(mysql.New(mysql.Config{
        Conn: db,
    }), &gorm.Config{
        Logger:         gormLogger,
        TranslateError: true,
        NamingStrategy: schema.NamingStrategy{
            SingularTable: true, // Use singular table names
        },
    })
sysmat commented 1 year ago
thomas-tacquet commented 1 year ago

Hello 👋 thank you for raising an issue and sorry for the response time 🤦

Gorm does not offers a "Debug" log level, to start the Debug mode on gorm you need to use the Debug function. I did a full example here to test if it works:


package main

import (
    "fmt"
    "os"

    log "github.com/sirupsen/logrus"
    gormv2logrus "github.com/thomas-tacquet/gormv2-logrus"
    "gorm.io/driver/sqlite"
    "gorm.io/gorm"
    "gorm.io/gorm/logger"
)

func main() {
    lrus := log.New()
    lrus.SetFormatter(&log.JSONFormatter{})
    lrus.SetOutput(os.Stdout)
    lrus.SetLevel(log.TraceLevel)

    gormLog := gormv2logrus.NewGormlog(
        gormv2logrus.WithLogrus(lrus),
        gormv2logrus.WithGormOptions(
            gormv2logrus.GormOptions{
                LogLevel:   logger.Info,
                LogLatency: true,
            },
        ),
    )

    const sqliteConnString = "file:%s?mode=memory&cache=shared"

    db, err := gorm.Open(sqlite.Open(
        fmt.Sprintf(sqliteConnString, "testing")),
        &gorm.Config{Logger: gormLog},
    )
    if err != nil {
        panic(err)
    }

    _ = db.Debug().Exec(`SELECT printf('This is an information message.');`)
    if err != nil {
        log.Fatal(err)
    }
}

``