gormv2logrus

package module
v1.3.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 15, 2022 License: MIT Imports: 8 Imported by: 0

README

gormv2-logrus

Easily connect Gorm V2 and logrus with customizable options

Usage example :

package test

import (
	gormv2logrus "github.com/CIDgravity/gormv2-logrus-fork"
	"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{
				SlowThreshold: slowThresholdDuration,
				LogLevel:      logger.Error,
				LogLatency:    true,
			},
		),
	)

Colorful option has been removed, because Logrus can't really handle the color code, especially if the logs are redirected to a file, the characters are not removed automatically

Contibuting

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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BannedKeyword

type BannedKeyword struct {
	// Keyword represent the string watched, for example : "password"
	Keyword string
	// CaseMatters if set to false, the Keyword matching will occur depending the case.
	// if set to true, Keyword will stricly match input messages
	CaseMatters bool
}

BannedKeyword represents a rule for scanning for Keyword in log output.

type GormOptions

type GormOptions struct {
	SlowThreshold time.Duration
	LogLevel      logger.LogLevel
	TruncateLen   uint
	LogLatency    bool
}

type Gormlog

type Gormlog struct {
	// SkipErrRecordNotFound if set to true, errors of type gorm.ErrRecordNotFound will be ignored.
	SkipErrRecordNotFound bool

	// SlowThreshold is used to determine a limit of slow requests, if a request time is above SlowThreshold,
	// it will be logged as warning.
	SlowThreshold time.Duration

	// SourceField if definied, source will appear in log with detailed file context.
	SourceField string

	LogLevel logger.LogLevel
	// contains filtered or unexported fields
}

Gormlog must match gorm logger.Interface to be compatible with gorm. Gormlog can be assigned in gorm configuration (see example in README.md).

func NewGormlog

func NewGormlog(opts ...Option) *Gormlog

NewGormlog create an instance of.

func (*Gormlog) Error

func (gl *Gormlog) Error(ctx context.Context, msg string, args ...interface{})

Error Gormlog of error log level

func (*Gormlog) Info

func (gl *Gormlog) Info(ctx context.Context, msg string, args ...interface{})

Info implementation of info log level

func (*Gormlog) LogMode

func (gl *Gormlog) LogMode(ll logger.LogLevel) logger.Interface

LogMode implementation log mode.

func (*Gormlog) Trace

func (gl *Gormlog) Trace(ctx context.Context, begin time.Time, fc func() (string, int64), err error)

Trace implementation of trace log level

func (*Gormlog) Warn

func (gl *Gormlog) Warn(ctx context.Context, msg string, args ...interface{})

Warn implementation of warn log level

type Option

type Option interface {
	// contains filtered or unexported methods
}

Option interface is used to configure gormv2_logrus options.

func WithBannedKeyword

func WithBannedKeyword(bannedKeywords []BannedKeyword) Option

func WithGormOptions

func WithGormOptions(gormOpt GormOptions) Option

func WithLogrus

func WithLogrus(lr *logrus.Logger) Option

WithLogrus Option is (not compatible with WithLogrusEntry) is used to set your logrus isntance.

func WithLogrusEntry

func WithLogrusEntry(logrusEntry *logrus.Entry) Option

WithLogrusEntry Option (not compatible with WithLogrus) used to specify your logrusEntry instance. If you don't set a logrusEntry isntance or if your logrusInstance is nil, Gormlog will consider that you want log to be printed on stdout. It's useful on developpement purposes when you want to see your logs directly in terminal.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL