chore: simplify setting log level

This commit is contained in:
andig 2022-10-14 09:46:01 +02:00
parent f787eaac0c
commit 9237baa09e
10 changed files with 21 additions and 22 deletions

View file

@ -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 {

View file

@ -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 {

View file

@ -1,7 +1,9 @@
network:
port: 7070
log: info
log: debug
levels:
mercedes: trace
interval: 3s
@ -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:
@ -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"

View file

@ -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 {

View file

@ -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 {

View file

@ -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 {

View file

@ -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 {

View file

@ -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") != "" {

View file

@ -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 {

View file

@ -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 {