Skip to content

logrus for gorm v, logger can add hooks and use them normally globally, supporting all the features that Logur itself supports. 一个支持添加为 gormv2 log 增加 hook 的 logrus 拓展库。

Notifications You must be signed in to change notification settings

FWangZil/gorm2logrus

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

gorm2logrus

logrus for gorm v2 base on sirupsen/logrus && onrik/gorm-logrus

example

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

About

logrus for gorm v, logger can add hooks and use them normally globally, supporting all the features that Logur itself supports. 一个支持添加为 gormv2 log 增加 hook 的 logrus 拓展库。

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages