vschst / gorm-logger

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

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

GORM Logger

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

Install

go get github.com/vschst/gorm-logger

Basic usage

package main

import (
    "github.com/vschst/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

© Viktor Schastnyy, since 2021

Released under the MIT License

About

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

License:MIT License


Languages

Language:Go 100.0%