Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

logging not working #9

Open
sysmat opened this issue Jun 9, 2023 · 2 comments
Open

logging not working #9

sysmat opened this issue Jun 9, 2023 · 2 comments

Comments

@sysmat
Copy link

sysmat commented Jun 9, 2023

  • mod
    go 1.20
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
)
  • code
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
		},
	})
  • in my log file I don't see gorm logging, I try gorm.Debug() no logging
  • when I use gorm logger logger.Default.LogMode(logger.Silent) it works fine, but output is console
@sysmat
Copy link
Author

sysmat commented Jun 9, 2023

  • I see in log sql error, so it works
  • Can I configure gormLogger to log gorm Debug()?

@thomas-tacquet
Copy link
Owner

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)
	}
}

``

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants