From 71584c65e7fec7d238f666064538e9e027da2574 Mon Sep 17 00:00:00 2001 From: Jacob Vecera Date: Thu, 12 Dec 2024 01:18:30 +0100 Subject: [PATCH] handle update in run --- cmd/pbm-agent/main.go | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/cmd/pbm-agent/main.go b/cmd/pbm-agent/main.go index 49e29f32d..3818cd5f6 100644 --- a/cmd/pbm-agent/main.go +++ b/cmd/pbm-agent/main.go @@ -22,8 +22,6 @@ import ( const mongoConnFlag = "mongodb-uri" -var logOpts *log.Opts - func main() { rootCmd := rootCommand() rootCmd.AddCommand(versionCommand()) @@ -35,12 +33,6 @@ func main() { viper.AddConfigPath(".") viper.WatchConfig() - viper.OnConfigChange(func(e fsnotify.Event) { - fmt.Printf("Config file changed: %s\n", e.Name) - *logOpts = *buildLogOpts() - fmt.Printf("Updated log options: %+v\n", logOpts) - }) - if err := viper.ReadInConfig(); err == nil { fmt.Println("Using config file:", viper.ConfigFileUsed()) } @@ -83,7 +75,7 @@ func rootCommand() *cobra.Command { hidecreds() - logOpts = buildLogOpts() + logOpts := buildLogOpts() l := log.NewWithOpts(nil, "", "", logOpts).NewDefaultEvent() @@ -196,6 +188,11 @@ func runAgent( logOpts) defer logger.Close() + viper.OnConfigChange(func(e fsnotify.Event) { + fmt.Printf("Config file changed: %s\n", e.Name) + logger.SetOpts(buildLogOpts()) + }) + ctx = log.SetLoggerToContext(ctx, logger) mtLog.SetDateFormat(log.LogTimeFormat)