Logging queries


For quick debugging, you can print executed queries to stdout. First, you need to install bundebug package:

go get github.com/uptrace/bun/extra/bundebug

Then add the provided query hook which by default only prints failed queries:

db := bun.NewDB(sqldb, dialect)

To print all queries, use WithVerbose option:


You can also disable the hook by default and use environment variables to enable it when needed:

    // disable the hook

    // BUNDEBUG=1 logs failed queries
    // BUNDEBUG=2 logs all queries

Logrus hook

You can also use logrusbun to log executed queries using Logrusopen in new window

go get github.com/oiime/logrusbun

Use QueryHookOptions to adjust log levels and behavior:

db := bun.NewDB(sqldb, dialect)

log := logrus.New()
db.AddQueryHook(logrusbun.NewQueryHook(logrusbun.QueryHookOptions{Logger: log}))
Get insights and updates in your inbox: