Files
cloud-services/pkg/db/sqllogger.go

35 lines
741 B
Go

package db
import (
"context"
"github.com/fiskerinc/cloud-services/pkg/logger"
pg "github.com/go-pg/pg/v10"
)
// SQLLogger query hook to display generated SQL
// To use:
// Driver.AddQueryHook(db.SQLLogger{})
// For development use only. Do not enable in production
type SQLLogger struct {
Out chan []byte
}
// AfterQuery query hook to display generated SQL
func (d SQLLogger) AfterQuery(c context.Context, q *pg.QueryEvent) error {
data, _ := q.FormattedQuery()
if d.Out != nil {
d.Out <- data
} else {
logger.Debug().Msgf("AfterQuery: %s", string(data))
}
return nil
}
// BeforeQuery required for interface
func (d SQLLogger) BeforeQuery(c context.Context, q *pg.QueryEvent) (context.Context, error) {
return c, nil
}