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

Introduce log levels per area #236

Merged
merged 1 commit into from
Jul 20, 2020
Merged
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
2 changes: 1 addition & 1 deletion charger/nrgble_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion charger/wallbe.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion cmd/charger.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
type config struct {
URI string
Log string
Levels map[string]string
Interval time.Duration
Mqtt mqttConfig
Influx server.InfluxConfig
Expand Down
2 changes: 1 addition & 1 deletion cmd/meter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
2 changes: 1 addition & 1 deletion cmd/vehicle.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions core/site.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down
6 changes: 6 additions & 0 deletions evcc.dist.yaml
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion meter/modbus.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion meter/tesla.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
Expand Down
2 changes: 1 addition & 1 deletion server/influxdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 != "" {
Expand Down
2 changes: 1 addition & 1 deletion server/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ package server

import "github.com/andig/evcc/util"

var log = util.NewLogger("srvr")
var log = util.NewLogger("server")
50 changes: 41 additions & 9 deletions util/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,47 @@ 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

// LogThreshold is the default log file level
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
}

// 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
Expand All @@ -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))
})
}

Expand Down