diff --git a/cmd/charger.go b/cmd/charger.go index 6d4a66104a..9df2cbc00c 100644 --- a/cmd/charger.go +++ b/cmd/charger.go @@ -42,6 +42,8 @@ func runCharger(cmd *cobra.Command, args []string) { log.FATAL.Fatal(err) } + setLogLevel(cmd) + // setup environment if err := configureEnvironment(cmd, conf); err != nil { log.FATAL.Fatal(err) diff --git a/cmd/charger_ramp.go b/cmd/charger_ramp.go index 6a51ab04d0..bf90222908 100644 --- a/cmd/charger_ramp.go +++ b/cmd/charger_ramp.go @@ -76,6 +76,8 @@ func runChargerRamp(cmd *cobra.Command, args []string) { log.FATAL.Fatal(err) } + setLogLevel(cmd) + // setup environment if err := configureEnvironment(cmd, conf); err != nil { log.FATAL.Fatal(err) diff --git a/cmd/discuss.go b/cmd/discuss.go index 473ea7d317..c6f36ee453 100644 --- a/cmd/discuss.go +++ b/cmd/discuss.go @@ -42,6 +42,8 @@ func runDiscuss(cmd *cobra.Command, args []string) { cfgErr := loadConfigFile(&conf) + setLogLevel(cmd) + file, pathErr := filepath.Abs(cfgFile) if pathErr != nil { file = cfgFile diff --git a/cmd/dump.go b/cmd/dump.go index 1a4eae537c..eda9f9e314 100644 --- a/cmd/dump.go +++ b/cmd/dump.go @@ -50,6 +50,8 @@ func runDump(cmd *cobra.Command, args []string) { // load config err := loadConfigFile(&conf) + setLogLevel(cmd) + // setup environment if err == nil { err = configureEnvironment(cmd, conf) diff --git a/cmd/flags.go b/cmd/flags.go index 7adaea1632..b543e1479f 100644 --- a/cmd/flags.go +++ b/cmd/flags.go @@ -36,20 +36,20 @@ const ( flagDelay = "delay" ) -func bind(cmd *cobra.Command, flag, vpr string) { - if err := viper.BindPFlag(vpr, cmd.Flags().Lookup(flag)); err != nil { +func bind(cmd *cobra.Command, flag string) { + if err := viper.BindPFlag(flag, cmd.Flags().Lookup(flag)); err != nil { panic(err) } } -func bindP(cmd *cobra.Command, flag, vpr string) { - if err := viper.BindPFlag(vpr, cmd.PersistentFlags().Lookup(flag)); err != nil { +func bindP(cmd *cobra.Command, flag string) { + if err := viper.BindPFlag(flag, cmd.PersistentFlags().Lookup(flag)); err != nil { panic(err) } } func selectByName(cmd *cobra.Command, conf *[]qualifiedConfig) error { - flag := cmd.PersistentFlags().Lookup(flagName) + flag := cmd.Flags().Lookup(flagName) if !flag.Changed { return nil } diff --git a/cmd/helper.go b/cmd/helper.go index ee35bb7afa..7cf2d43861 100644 --- a/cmd/helper.go +++ b/cmd/helper.go @@ -11,8 +11,20 @@ import ( "time" "github.com/evcc-io/evcc/cmd/shutdown" + "github.com/evcc-io/evcc/util" + "github.com/spf13/cobra" + "github.com/spf13/viper" ) +// setLogLevel sets log level from config overwritten by command line +// https://github.com/spf13/viper/issues/1444 +func setLogLevel(cmd *cobra.Command) { + if flag := cmd.Flags().Lookup("log"); viper.GetString("log") == "" || flag.Changed { + viper.Set("log", flag.Value.String()) + } + util.LogLevel(viper.GetString("log"), viper.GetStringMapString("levels")) +} + // unwrap converts a wrapped error into slice of strings func unwrap(err error) (res []string) { for err != nil { diff --git a/cmd/meter.go b/cmd/meter.go index 0953b29356..486197cf0c 100644 --- a/cmd/meter.go +++ b/cmd/meter.go @@ -31,6 +31,8 @@ func runMeter(cmd *cobra.Command, args []string) { log.FATAL.Fatal(err) } + setLogLevel(cmd) + // setup environment if err := configureEnvironment(cmd, conf); err != nil { log.FATAL.Fatal(err) diff --git a/cmd/root.go b/cmd/root.go index 3023f3ebf3..6dbf4df6db 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -53,13 +53,13 @@ func init() { // config file options rootCmd.PersistentFlags().StringP("log", "l", "info", "Log level (fatal, error, warn, info, debug, trace)") - bindP(rootCmd, "log", "log") + bindP(rootCmd, "log") rootCmd.Flags().Bool("metrics", false, "Expose metrics") - bind(rootCmd, "metrics", "metrics") + bind(rootCmd, "metrics") rootCmd.Flags().Bool("profile", false, "Expose pprof profiles") - bind(rootCmd, "profile", "profile") + bind(rootCmd, "profile") } // initConfig reads in config file and ENV variables if set @@ -110,7 +110,7 @@ func runRoot(cmd *cobra.Command, args []string) { err = cfgErr } - util.LogLevel(viper.GetString("log"), viper.GetStringMapString("levels")) + setLogLevel(cmd) // network config if viper.GetString("uri") != "" { diff --git a/cmd/token.go b/cmd/token.go index 9346f8373a..36f4b10a46 100644 --- a/cmd/token.go +++ b/cmd/token.go @@ -33,6 +33,8 @@ func runToken(cmd *cobra.Command, args []string) { log.FATAL.Fatal(err) } + setLogLevel(cmd) + var vehicleConf qualifiedConfig if len(conf.Vehicles) == 1 { vehicleConf = conf.Vehicles[0] diff --git a/cmd/vehicle.go b/cmd/vehicle.go index 52cac5624f..b9dfa8fd08 100644 --- a/cmd/vehicle.go +++ b/cmd/vehicle.go @@ -34,6 +34,8 @@ func runVehicle(cmd *cobra.Command, args []string) { fatal(err) } + setLogLevel(cmd) + // setup environment if err := configureEnvironment(cmd, conf); err != nil { fatal(err)