Alternative logging with zap for GORM ⚡️
In comparison to gorm's default logger, gormzap
is faster, reflection free, low allocations and no regex compilations.
package main
import (
"github.com/jinzhu/gorm"
"github.com/wantedly/gorm-zap"
)
const (
databaseURL = "postgres://postgres:@localhost/gormzap?sslmode=disable"
)
func main() {
logger, err = zap.NewProduction()
if err != nil {
panic(err)
}
db, err := gorm.Open("postgres", databaseURL)
if err != nil {
panic(err)
}
db.LogMode(true)
db.SetLogger(gormzap.New(logger))
// ...
}
According to our benchmark, gormzap
makes DB operations at least 5% faster and reduce object allocations.
Logger | Time | Object Allocated |
---|---|---|
default | 187940 ns/op | 494 allocs/op |
gormzap | 185383 ns/op | 475 allocs/op |
Logger | Time | Object Allocated |
---|---|---|
default | 169361 ns/op | 531 allocs/op |
gormzap | 151304 ns/op | 519 allocs/op |
Logger | Time | Object Allocated |
---|---|---|
default | 200632 ns/op | 720 allocs/op |
gormzap | 190732 ns/op | 645 allocs/op |
Logger | Time | Object Allocated |
---|---|---|
default | 444513 ns/op | 1723 allocs/op |
gormzap | 263098 ns/op | 1101 allocs/op |