Skip to content

Easily connect Gorm V2 and logrus with customizable options

License

Notifications You must be signed in to change notification settings

FlorianRuen/gormv2-logrus

 
 

Repository files navigation

gormv2-logrus

Easily connect Gorm V2 and logrus with customizable options

Usage example :

package test

import (
	gormv2logrus "github.com/thomas-tacquet/gormv2-logrus"
	"gorm.io/gorm"
	"gorm.io/gorm/logger"
)

func myFunctionToConnectOnMyDB(e *logrus.Entry) {
	gormLogger := gormv2logrus.NewGormlog(gormv2logrus.WithLogrusEntry(e))
	gormLogger.LogMode(logger.Error)

	gormConfig := &gorm.Config{
		Logger:                 gormLogger,
		CreateBatchSize:        1500,
		SkipDefaultTransaction: true,
	}
	
	db, err := gorm.Open(
		"CONNEXION STRING",
		gormConfig,
	)
}

Or you can use with a logrus.Logger :

gormLogger := gormv2logrus.NewGormlog(gormv2logrus.WithLogrus(e))

You can also add customization options :

	slowThresholdDuration, _ := time.ParseDuration("300ms")

	gormLogger := gormv2logrus.NewGormlog(
		gormv2logrus.WithLogrus(e),
		gormv2logrus.WithGormOptions(
			gormv2logrus.GormOptions{
				Colorful:      true,
				SlowThreshold: slowThresholdDuration,
				LogLevel:      logger.Error,
				LogLatency:    true,
			},
		),
	)

Contibuting

Just feel free to open issues, ask questions, make proposals.

About

Easily connect Gorm V2 and logrus with customizable options

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Go 100.0%