Skip to content

A customized GORM logger that implements the appropriate interface and uses Logrus to output logs

License

Notifications You must be signed in to change notification settings

mathandcrypto/cryptomath-gorm-logger

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

CryptoMath GORM Logger

A customized GORM logger that implements the appropriate interface and uses Logrus to output logs.

Install

go get github.com/mathandcrypto/cryptomath-gorm-logger

Basic usage

package main

import (
    "github.com/mathandcrypto/cryptomath-gorm-logger"
    "github.com/sirupsen/logrus"
    "gorm.io/driver/sqlite"
    "gorm.io/gorm"
    gormLogger "gorm.io/gorm/logger"
    "time"
)

func main() {
    log := logrus.New()
    newLogger := logger.New(log, logger.Config{
        SlowThreshold:  time.Second,
        SkipErrRecordNotFound: true,
        LogLevel:   gormLogger.Error,
    })

    db, err := gorm.Open(sqlite.Open("test.db"), &gorm.Config{
        Logger: newLogger,
    })
}

Logger сonfiguration

When creating in the logger.New method, the second parameter specifies the configuration of the logger. The logger.Config structure has the following fields:

Parameter Type Default value Description
SlowThreshold time.Duration If the sql query time exceeds this value, a warning log about the slow sql query time will be output
SkipErrRecordNotFound bool false Skip ErrRecordNotFound error for logger
SourceField string Source field in config which is recorded file name and line number of the current error
ModuleName string "gorm" Name of the module in the log
LogLevel gormLogger.LogLevel gormLogger.Info Log level

License

© CryptoMath, since 2021

Released under the MIT License

About

A customized GORM logger that implements the appropriate interface and uses Logrus to output logs

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages