Skip to content

Instantly share code, notes, and snippets.

@dreh23
Forked from bnadland/Using logrus and gorm
Created July 11, 2016 11:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dreh23/9d550b6564989999a7091eaed0cf5837 to your computer and use it in GitHub Desktop.
Save dreh23/9d550b6564989999a7091eaed0cf5837 to your computer and use it in GitHub Desktop.
import (
log "github.com/sirupsen/logrus"
"github.com/jinzhu/gorm"
)
type GormLogger struct {}
func (*GormLogger) Print(v ...interface{}) {
if v[0] == "sql" {
log.WithFields(log.Fields{"module": "gorm", "type": "sql"}).Print(v[3])
}
if v[0] == "log" {
log.WithFields(log.Fields{"module": "gorm", "type": "log"}).Print(v[2])
}
}
func main() {
db, _ := gorm.Open(...)
defer db.Close()
db.SetLogger(&GormLogger{})
db.LogMode(true)
...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment