From 75a0918a7ef8bb6c9506de69dd4e6b6e8c35e567 Mon Sep 17 00:00:00 2001 From: sentriz Date: Fri, 24 Oct 2025 17:05:24 +0100 Subject: [PATCH] feat(gonic): add log-db option to log queries related #620 --- cmd/gonic/gonic.go | 3 ++- db/db.go | 7 +++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/cmd/gonic/gonic.go b/cmd/gonic/gonic.go index 67f359e..ffee862 100644 --- a/cmd/gonic/gonic.go +++ b/cmd/gonic/gonic.go @@ -88,6 +88,7 @@ func main() { confPprof := flag.Bool("pprof", false, "enable the /debug/pprof endpoint (optional)") confExpvar := flag.Bool("expvar", false, "enable the /debug/vars endpoint (optional)") + confLogDB := flag.Bool("log-db", false, "enable database query logging (optional)") deprecatedConfGenreSplit := flag.String("genre-split", "", "(deprecated, see multi-value settings)") @@ -137,7 +138,7 @@ func main() { log.Fatalf("couldn't create covers cache path: %v\n", err) } - dbc, err := db.New(*confDBPath, db.DefaultOptions()) + dbc, err := db.New(*confDBPath, db.DefaultOptions(), *confLogDB) if err != nil { log.Fatalf("error opening database: %v\n", err) } diff --git a/db/db.go b/db/db.go index fc5545f..369e4b5 100644 --- a/db/db.go +++ b/db/db.go @@ -40,7 +40,7 @@ type DB struct { *gorm.DB } -func New(path string, options url.Values) (*DB, error) { +func New(path string, options url.Values, logQueries bool) (*DB, error) { url := url.URL{ Scheme: "file", Opaque: path, @@ -51,12 +51,15 @@ func New(path string, options url.Values) (*DB, error) { return nil, fmt.Errorf("with gorm: %w", err) } db.SetLogger(log.New(os.Stdout, "gorm ", 0)) + if logQueries { + db.LogMode(true) + } db.DB().SetMaxOpenConns(1) return &DB{DB: db}, nil } func NewMock() (*DB, error) { - return New(":memory:", mockOptions()) + return New(":memory:", mockOptions(), false) } func (db *DB) InsertBulkLeftMany(table string, head []string, left int, col []int) error {