From 01a3ed0d3e401d45b52480f1013fd0c0cfc2ba00 Mon Sep 17 00:00:00 2001 From: Agniva De Sarker Date: Fri, 11 Feb 2022 12:37:41 +0530 Subject: [PATCH] MM-41143: Pass a custom logger to morph (#19530) We create a custom io.Writer to pass to the logger instance. For now, we keep everything to debug as all errors are surfaced back to the server via the API. ```release-note NONE ``` --- store/sqlstore/store.go | 8 ++++++-- store/sqlstore/utils.go | 11 +++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/store/sqlstore/store.go b/store/sqlstore/store.go index 04f11cc096..121e2001cb 100644 --- a/store/sqlstore/store.go +++ b/store/sqlstore/store.go @@ -8,6 +8,7 @@ import ( "database/sql" dbsql "database/sql" "fmt" + "log" "path/filepath" "strconv" "strings" @@ -27,7 +28,6 @@ import ( _ "github.com/golang-migrate/migrate/v4/source/file" "github.com/jmoiron/sqlx" "github.com/lib/pq" - _ "github.com/lib/pq" "github.com/mattermost/gorp" "github.com/pkg/errors" @@ -1012,7 +1012,11 @@ func (ss *SqlStore) migrate(direction migrationDirection) error { return err } - engine, err := morph.New(context.Background(), driver, src, morph.WithLock("mm-lock-key")) + opts := []morph.EngineOption{ + morph.WithLogger(log.New(&morphWriter{}, "", log.Lshortfile)), + morph.WithLock("mm-lock-key"), + } + engine, err := morph.New(context.Background(), driver, src, opts...) if err != nil { return err } diff --git a/store/sqlstore/utils.go b/store/sqlstore/utils.go index 0e63f0f1c7..e3f57513d3 100644 --- a/store/sqlstore/utils.go +++ b/store/sqlstore/utils.go @@ -147,3 +147,14 @@ func constructArrayArgs(ids []string) (string, []interface{}) { return "(" + placeholder.String() + ")", values } + +// morphWriter is a target to pass to the logger instance of morph. +// For now, everything is just logged at a debug level. If we need to log +// errors/warnings from the library also, that needs to be seen later. +type morphWriter struct { +} + +func (l *morphWriter) Write(in []byte) (int, error) { + mlog.Debug(string(in)) + return len(in), nil +}