Skip to main content

日志 (Logger)

GORM 有一个默认的 logger 实现。

启用日志

默认情况下,GORM 仅在发生错误时打印日志。你可以通过 LogMode 更改日志级别。

// Silent, Error, Warn, Info
newLogger := logger.Default.LogMode(logger.Info)

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

自定义 Logger

GORM 的 Logger 是一个接口,你可以实现自己的 Logger。

type Interface interface {
LogMode(LogLevel) Interface
Info(context.Context, string, ...interface{})
Warn(context.Context, string, ...interface{})
Error(context.Context, string, ...interface{})
Trace(ctx context.Context, begin time.Time, fc func() (sql string, rowsAffected int64), err error)
}