A customized GORM logger that implements the appropriate interface and uses Logrus to output logs.
go get github.com/vschst/gorm-logger
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,
})
}
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 |
© Viktor Schastnyy, since 2021
Released under the MIT License