Skip to content

Commit

Permalink
chore: simplify setting log level
Browse files Browse the repository at this point in the history
  • Loading branch information
andig committed Oct 14, 2022
1 parent f787eaa commit 9237baa
Show file tree
Hide file tree
Showing 10 changed files with 21 additions and 22 deletions.
2 changes: 1 addition & 1 deletion cmd/charger.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func runCharger(cmd *cobra.Command, args []string) {
log.FATAL.Fatal(err)
}

setLogLevel(cmd)
util.LogLevel(viper.GetString("log"), viper.GetStringMapString("levels"))

// setup environment
if err := configureEnvironment(cmd, conf); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion cmd/charger_ramp.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func runChargerRamp(cmd *cobra.Command, args []string) {
log.FATAL.Fatal(err)
}

setLogLevel(cmd)
util.LogLevel(viper.GetString("log"), viper.GetStringMapString("levels"))

// setup environment
if err := configureEnvironment(cmd, conf); err != nil {
Expand Down
15 changes: 13 additions & 2 deletions cmd/demo.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
network:
port: 7070

log: info
log: debug
levels:
mercedes: trace

interval: 3s

Expand Down Expand Up @@ -198,6 +200,15 @@ vehicles:
title: grüner Honda e
capacity: 26

- name: mercedes1
type: mercedes
title: Mercedes
phases: 2
vin: W1K2052131G009532
clientId: 53c28422-25e4-47c2-9cf4-94a48065c5b3
clientSecret: ePhxFTNyMbvslPqgoAfERHscIvpHZIaOfKPLjmEvQZDhSaocKAOiceLEVubDPyxP
identifiers: Jochen

site:
title: Zuhause
meters:
Expand All @@ -211,7 +222,7 @@ loadpoints:
mode: pv
phases: 1
meter: meter_charger_1
vehicle: vehicle_1 # default
# vehicle: vehicle_1 # default
- title: Garage
charger: charger_2
mode: "off"
Expand Down
2 changes: 1 addition & 1 deletion cmd/discuss.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func runDiscuss(cmd *cobra.Command, args []string) {

cfgErr := loadConfigFile(&conf)

setLogLevel(cmd)
util.LogLevel(viper.GetString("log"), viper.GetStringMapString("levels"))

file, pathErr := filepath.Abs(cfgFile)
if pathErr != nil {
Expand Down
2 changes: 1 addition & 1 deletion cmd/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func runDump(cmd *cobra.Command, args []string) {
// load config
err := loadConfigFile(&conf)

setLogLevel(cmd)
util.LogLevel(viper.GetString("log"), viper.GetStringMapString("levels"))

// setup environment
if err == nil {
Expand Down
12 changes: 0 additions & 12 deletions cmd/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,8 @@ 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 {
Expand Down
2 changes: 1 addition & 1 deletion cmd/meter.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func runMeter(cmd *cobra.Command, args []string) {
log.FATAL.Fatal(err)
}

setLogLevel(cmd)
util.LogLevel(viper.GetString("log"), viper.GetStringMapString("levels"))

// setup environment
if err := configureEnvironment(cmd, conf); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func runRoot(cmd *cobra.Command, args []string) {
err = cfgErr
}

setLogLevel(cmd)
util.LogLevel(viper.GetString("log"), viper.GetStringMapString("levels"))

// network config
if viper.GetString("uri") != "" {
Expand Down
2 changes: 1 addition & 1 deletion cmd/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func runToken(cmd *cobra.Command, args []string) {
log.FATAL.Fatal(err)
}

setLogLevel(cmd)
util.LogLevel(viper.GetString("log"), viper.GetStringMapString("levels"))

var vehicleConf qualifiedConfig
if len(conf.Vehicles) == 1 {
Expand Down
2 changes: 1 addition & 1 deletion cmd/vehicle.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func runVehicle(cmd *cobra.Command, args []string) {
fatal(err)
}

setLogLevel(cmd)
util.LogLevel(viper.GetString("log"), viper.GetStringMapString("levels"))

// setup environment
if err := configureEnvironment(cmd, conf); err != nil {
Expand Down

0 comments on commit 9237baa

Please sign in to comment.