logrus for gorm v2 base on sirupsen/logrus && onrik/gorm-logrus
go get github.com/FWangZil/gorm2logrus@main
package main
import github.com/FWangZil/gorm2logrus
// Logger can add hooks and use them normally globally, supporting all the features that logrus itself supports
var Logger *logrus.Logger
// Custom Logger for use by GORM v2
var GormLogger *gorm2logrus.GormLogger
var DB *gorm.DB
func main(){
initLogrus()
initDataBase(GormLogger)
}
func initLogrus() {
logger := gorm2logrus.NewGormLogger()
logger.SetReportCaller(true)
logger.SetFormatter(&logrus.JSONFormatter{
PrettyPrint: true,
})
GormLogger = logger
Logger = &GormLogger.Logger
}
func initDataBase(gormLogger *gorm2logrus.GormLogger) {
var err error
gormConfig := &gorm.Config{
DisableForeignKeyConstraintWhenMigrating: true,
}
if gormLogger != nil {
gormConfig.Logger = gormLogger
}
// https://github.com/go-gorm/postgres
DB, err = gorm.Open(postgres.New(postgres.Config{
DSN: "dsn",
PreferSimpleProtocol: true, // disables implicit prepared statement usage
}), gormConfig)
if err != nil {
Logger.Fatalf("pgsql connect failed: %v", err)
}
}