Introduce log levels per area (#236)
This commit is contained in:
parent
85b47cbe70
commit
7c864df08a
14 changed files with 60 additions and 20 deletions
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -14,6 +14,7 @@ import (
|
|||
type config struct {
|
||||
URI string
|
||||
Log string
|
||||
Levels map[string]string
|
||||
Interval time.Duration
|
||||
Mqtt mqttConfig
|
||||
Influx server.InfluxConfig
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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()
|
||||
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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))
|
||||
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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),
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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 != "" {
|
||||
|
|
|
|||
|
|
@ -2,4 +2,4 @@ package server
|
|||
|
||||
import "github.com/andig/evcc/util"
|
||||
|
||||
var log = util.NewLogger("srvr")
|
||||
var log = util.NewLogger("server")
|
||||
|
|
|
|||
50
util/log.go
50
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))
|
||||
})
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue