diff --git a/charger/nrgble_linux.go b/charger/nrgble_linux.go index f74f5bc2c8..b9ad2b1ce5 100644 --- a/charger/nrgble_linux.go +++ b/charger/nrgble_linux.go @@ -52,7 +52,7 @@ func NewNRGKickBLEFromConfig(log *util.Logger, other map[string]interface{}) api // NewNRGKickBLE creates NRGKickBLE charger func NewNRGKickBLE(device, macaddress string, pin int) *NRGKickBLE { - logger := util.NewLogger("nrgbt") + logger := util.NewLogger("nrg-bt") // set LE mode btmgmt := hw.NewBtMgmt(device) diff --git a/charger/wallbe.go b/charger/wallbe.go index 457984b394..94931d5dc4 100644 --- a/charger/wallbe.go +++ b/charger/wallbe.go @@ -67,7 +67,7 @@ func NewWallbe(conn string) *Wallbe { handler.ProtocolRecoveryTimeout = protocolTimeout wb := &Wallbe{ - log: util.NewLogger("wlbe"), + log: util.NewLogger("wallbe"), client: client, handler: handler, factor: 10, diff --git a/cmd/charger.go b/cmd/charger.go index 6c1b957360..0268bac31b 100644 --- a/cmd/charger.go +++ b/cmd/charger.go @@ -23,7 +23,7 @@ func init() { } func runCharger(cmd *cobra.Command, args []string) { - util.LogLevel(viper.GetString("log")) + util.LogLevel(viper.GetString("log"), viper.GetStringMapString("levels")) log.INFO.Printf("evcc %s (%s)", server.Version, server.Commit) // load config diff --git a/cmd/config.go b/cmd/config.go index 6fc90249e6..45ab570962 100644 --- a/cmd/config.go +++ b/cmd/config.go @@ -14,6 +14,7 @@ import ( type config struct { URI string Log string + Levels map[string]string Interval time.Duration Mqtt mqttConfig Influx server.InfluxConfig diff --git a/cmd/meter.go b/cmd/meter.go index 244d4c7f88..5cd916ca75 100644 --- a/cmd/meter.go +++ b/cmd/meter.go @@ -23,7 +23,7 @@ func init() { } func runMeter(cmd *cobra.Command, args []string) { - util.LogLevel(viper.GetString("log")) + util.LogLevel(viper.GetString("log"), viper.GetStringMapString("levels")) log.INFO.Printf("evcc %s (%s)", server.Version, server.Commit) // load config diff --git a/cmd/root.go b/cmd/root.go index 521ddcc19e..7eaabe18a7 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -129,12 +129,12 @@ func checkVersion() { } func run(cmd *cobra.Command, args []string) { - util.LogLevel(viper.GetString("log")) + util.LogLevel(viper.GetString("log"), viper.GetStringMapString("levels")) log.INFO.Printf("evcc %s (%s)", server.Version, server.Commit) // load config and re-configure logging after reading config file conf := loadConfigFile(cfgFile) - util.LogLevel(viper.GetString("log")) + util.LogLevel(viper.GetString("log"), viper.GetStringMapString("levels")) go checkVersion() diff --git a/cmd/vehicle.go b/cmd/vehicle.go index c6b0323cc7..5bfc6338cd 100644 --- a/cmd/vehicle.go +++ b/cmd/vehicle.go @@ -23,7 +23,7 @@ func init() { } func runVehicle(cmd *cobra.Command, args []string) { - util.LogLevel(viper.GetString("log")) + util.LogLevel(viper.GetString("log"), viper.GetStringMapString("levels")) log.INFO.Printf("evcc %s (%s)", server.Version, server.Commit) // load config diff --git a/core/site.go b/core/site.go index 5bc22f61db..269b99d712 100644 --- a/core/site.go +++ b/core/site.go @@ -299,6 +299,7 @@ func (site *Site) sitePower() (float64, error) { } func (site *Site) update(lp Updater) { + site.log.DEBUG.Println("----") mode := site.GetMode() site.publish("mode", string(mode)) diff --git a/evcc.dist.yaml b/evcc.dist.yaml index c2ae45c889..287361fe1e 100644 --- a/evcc.dist.yaml +++ b/evcc.dist.yaml @@ -1,6 +1,12 @@ uri: 0.0.0.0:7070 # uri for ui interval: 10s # control cycle interval +log: error +levels: + core: debug + lp-1: debug + lp-2: debug + # mqtt message broker mqtt: broker: localhost:1883 diff --git a/meter/modbus.go b/meter/modbus.go index e3f0773952..07b60e3e93 100644 --- a/meter/modbus.go +++ b/meter/modbus.go @@ -36,7 +36,7 @@ func NewModbusFromConfig(log *util.Logger, other map[string]interface{}) api.Met conn := modbus.NewConnection(log, cc.URI, cc.Device, cc.Comset, cc.Baudrate, *cc.RTU) device, err := modbus.NewDevice(log, cc.Model, *cc.RTU) - log = util.NewLogger("modb") + log = util.NewLogger("modbus") conn.Logger(log.TRACE) // prepare device diff --git a/meter/tesla.go b/meter/tesla.go index f6b6241c5a..8bfb2bdb82 100644 --- a/meter/tesla.go +++ b/meter/tesla.go @@ -61,7 +61,7 @@ func NewTeslaFromConfig(log *util.Logger, other map[string]interface{}) api.Mete // NewTesla creates a Tesla Meter func NewTesla(uri, usage string) api.Meter { m := &Tesla{ - HTTPHelper: util.NewHTTPHelper(util.NewLogger("tsla")), + HTTPHelper: util.NewHTTPHelper(util.NewLogger("tesla")), uri: uri, usage: strings.ToLower(usage), } diff --git a/server/influxdb.go b/server/influxdb.go index fb86b130a7..88139a0582 100644 --- a/server/influxdb.go +++ b/server/influxdb.go @@ -32,7 +32,7 @@ type Influx struct { // NewInfluxClient creates new publisher for influx func NewInfluxClient(url, token, org, user, password, database string) *Influx { - log := util.NewLogger("iflx") + log := util.NewLogger("influx") // InfluxDB v1 compatibility if token == "" && user != "" { diff --git a/server/log.go b/server/log.go index 01e6f6be84..efff712e19 100644 --- a/server/log.go +++ b/server/log.go @@ -2,4 +2,4 @@ package server import "github.com/andig/evcc/util" -var log = util.NewLogger("srvr") +var log = util.NewLogger("server") diff --git a/util/log.go b/util/log.go index 3f9ba26d16..6f731914fc 100644 --- a/util/log.go +++ b/util/log.go @@ -8,13 +8,17 @@ import ( "regexp" "strconv" "strings" + "sync" jww "github.com/spf13/jwalterweatherman" ) -var loggers = map[string]*Logger{} - var ( + loggers = map[string]*Logger{} + levels = map[string]jww.Threshold{} + + loggersMux sync.Mutex + // OutThreshold is the default console log level OutThreshold = jww.LevelError @@ -22,6 +26,8 @@ var ( LogThreshold = jww.LevelWarn ) +const padding = 6 // padding of log areas + // Logger wraps a jww notepad to avoid leaking implementation detail type Logger struct { *jww.Notepad @@ -29,10 +35,20 @@ type Logger struct { // NewLogger creates a logger with the given log area and adds it to the registry func NewLogger(area string) *Logger { - notepad := jww.NewNotepad(OutThreshold, LogThreshold, os.Stdout, ioutil.Discard, area, log.Ldate|log.Ltime) - l := &Logger{notepad} - loggers[area] = l - return l + padded := area + for len(padded) < padding { + padded = padded + " " + } + + level := LogLevelForArea(area) + notepad := jww.NewNotepad(level, level, os.Stdout, ioutil.Discard, padded, log.Ldate|log.Ltime) + + loggersMux.Lock() + defer loggersMux.Unlock() + + logger := &Logger{notepad} + loggers[area] = logger + return logger } // Loggers invokes callback for each configured logger @@ -42,13 +58,29 @@ func Loggers(cb func(string, *Logger)) { } } +// LogLevelForArea gets the log level for given log area +func LogLevelForArea(area string) jww.Threshold { + level, ok := levels[strings.ToLower(area)] + if !ok { + level = OutThreshold + } + return level +} + // LogLevel sets log level for all loggers -func LogLevel(level string) { - OutThreshold = LogLevelToThreshold(level) +func LogLevel(defaultLevel string, areaLevels map[string]string) { + // default level + OutThreshold = LogLevelToThreshold(defaultLevel) LogThreshold = OutThreshold + // area levels + for area, level := range areaLevels { + area = strings.ToLower(area) + levels[area] = LogLevelToThreshold(level) + } + Loggers(func(name string, logger *Logger) { - logger.SetStdoutThreshold(OutThreshold) + logger.SetStdoutThreshold(LogLevelForArea(name)) }) }