From 64f1978b93726bc6eb1661c1e1bd421cbb4ff58f Mon Sep 17 00:00:00 2001 From: Eray Ates Date: Mon, 17 Jun 2024 17:36:53 +0200 Subject: [PATCH] fix: logger with config Signed-off-by: Eray Ates --- config.go | 4 ++++ igmigrator.go | 11 ++++++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/config.go b/config.go index 5dc9136..d20d12b 100644 --- a/config.go +++ b/config.go @@ -4,6 +4,8 @@ import ( "os" "regexp" "strings" + + "github.com/worldline-go/logz" ) // Config provides a way to specify some optional configuration. @@ -39,6 +41,8 @@ type Config struct { // Values for expand function in migration files. Values map[string]string + + Logger logz.Adapter } // SetDefaults will update missing values with default ones(if any). diff --git a/igmigrator.go b/igmigrator.go index 5d5a91a..f679570 100644 --- a/igmigrator.go +++ b/igmigrator.go @@ -14,6 +14,7 @@ import ( "strings" "github.com/rs/zerolog" + "github.com/rs/zerolog/log" "github.com/worldline-go/logz" ) @@ -114,7 +115,15 @@ func MigrateInTx(ctx context.Context, tx Transaction, cnf *Config) (*MigrateResu migration := Migrator{ Cnf: cnf, Tx: tx, - Logger: logz.AdapterKV{Log: *zerolog.Ctx(ctx)}, + Logger: cnf.Logger, + } + + if migration.Logger == nil { + if zlog := zerolog.Ctx(ctx); zlog != nil { + migration.Logger = logz.AdapterKV{Log: *zlog, Caller: true} + } else { + migration.Logger = logz.AdapterKV{Log: log.Logger, Caller: true} + } } if err := migration.SetSchema(ctx); err != nil {