Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change log severity if config file gets updated. #2519

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 40 additions & 15 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@ import (
"fmt"
"log"
"os"
"reflect"
"strings"

"github.com/fsnotify/fsnotify"
"github.com/googlecloudplatform/gcsfuse/v2/cfg"
"github.com/googlecloudplatform/gcsfuse/v2/common"
"github.com/googlecloudplatform/gcsfuse/v2/internal/logger"
"github.com/googlecloudplatform/gcsfuse/v2/internal/util"
"github.com/mitchellh/mapstructure"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -73,22 +76,25 @@ of Cloud Storage FUSE, see https://cloud.google.com/storage/docs/gcs-fuse.`,
return
}
}
v.WatchConfig()
v.OnConfigChange(func(in fsnotify.Event) {
var newConfig cfg.Config
if err := parseConfig(v, &newConfig); err != nil {
logger.Errorf("Error while parsing config: %v", err)
return
}
// Currently, we only handle log-severity, so let's check if that works.
tempConfig := newConfig
tempConfig.Logging.Severity = configObj.Logging.Severity
if !reflect.DeepEqual(tempConfig, configObj) {
logger.Errorf("Irreconcilable differences exist between the old and the new configs. Please restart the app for them to take effect.")
}
configObj.Logging.Severity = newConfig.Logging.Severity
logger.SetLogSeverity(configObj.Logging.Severity)

})
cfgErr = parseConfig(v, &configObj)

if cfgErr = v.Unmarshal(&configObj, viper.DecodeHook(cfg.DecodeHook()), func(decoderConfig *mapstructure.DecoderConfig) {
// By default, viper supports mapstructure tags for unmarshalling. Override that to support yaml tag.
decoderConfig.TagName = "yaml"
// Reject the config file if any of the fields in the YAML don't map to the struct.
decoderConfig.ErrorUnused = true
},
); cfgErr != nil {
return
}
if cfgErr = cfg.ValidateConfig(v, &configObj); cfgErr != nil {
return
}
if cfgErr = cfg.Rationalize(v, &configObj); cfgErr != nil {
return
}
}
cobra.OnInitialize(initConfig)
rootCmd.PersistentFlags().StringVar(&cfgFile, "config-file", "", "The path to the config file where all gcsfuse related config needs to be specified. "+
Expand All @@ -101,6 +107,25 @@ of Cloud Storage FUSE, see https://cloud.google.com/storage/docs/gcs-fuse.`,
return rootCmd, nil
}

func parseConfig(v *viper.Viper, c *cfg.Config) error {
if err := v.Unmarshal(c, viper.DecodeHook(cfg.DecodeHook()), func(decoderConfig *mapstructure.DecoderConfig) {
// By default, viper supports mapstructure tags for unmarshalling. Override that to support yaml tag.
decoderConfig.TagName = "yaml"
// Reject the config file if any of the fields in the YAML don't map to the struct.
decoderConfig.ErrorUnused = true
},
); err != nil {
return err
}
if err := cfg.ValidateConfig(v, c); err != nil {
return err
}
if err := cfg.Rationalize(v, c); err != nil {
return err
}
return nil
}

// ConvertToPosixArgs converts a slice of commandline args and transforms them
// into POSIX compliant args. All it does is that it converts flags specified
// using a single-hyphen to double-hyphens. We are excluding "-v" because it's
Expand Down
5 changes: 5 additions & 0 deletions internal/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,11 @@ func SetLogFormat(format string) {
defaultLogger = defaultLoggerFactory.newLogger(defaultLoggerFactory.level)
}

func SetLogSeverity(severity cfg.LogSeverity) {
defaultLoggerFactory.level = string(severity)
defaultLogger = defaultLoggerFactory.newLogger(defaultLoggerFactory.level)
}

// Close closes the log file when necessary.
func Close() {
if f := defaultLoggerFactory.file; f != nil {
Expand Down
Loading