Update and improve logging library to logrus

This commit is contained in:
Nicholas Thompson
2020-06-15 13:28:03 +02:00
parent c459fb22aa
commit 6ab917d35a
5 changed files with 55 additions and 38 deletions

View File

@@ -1,12 +1,12 @@
package cli
import (
"fmt"
"log"
"github.com/diebietse/invertergui/mk2driver"
"github.com/sirupsen/logrus"
)
var log = logrus.WithField("ctx", "inverter-gui-cli")
type Cli struct {
mk2driver.Mk2
}
@@ -27,21 +27,21 @@ func (c *Cli) run() {
}
func printInfo(info *mk2driver.Mk2Info) {
out := fmt.Sprintf("Version: %v\n", info.Version)
out += fmt.Sprintf("Bat Volt: %.2fV Bat Cur: %.2fA \n", info.BatVoltage, info.BatCurrent)
out += fmt.Sprintf("In Volt: %.2fV In Cur: %.2fA In Freq %.2fHz\n", info.InVoltage, info.InCurrent, info.InFrequency)
out += fmt.Sprintf("Out Volt: %.2fV Out Cur: %.2fA Out Freq %.2fHz\n", info.OutVoltage, info.OutCurrent, info.OutFrequency)
out += fmt.Sprintf("In Power %.2fW Out Power %.2fW\n", info.InVoltage*info.InCurrent, info.OutVoltage*info.OutCurrent)
out += fmt.Sprintf("Charge State: %.2f%%\n", info.ChargeState*100)
out += "LEDs state:"
log.Infof("Version: %v", info.Version)
log.Infof("Bat Volt: %.2fV Bat Cur: %.2fA", info.BatVoltage, info.BatCurrent)
log.Infof("In Volt: %.2fV In Cur: %.2fA In Freq %.2fHz", info.InVoltage, info.InCurrent, info.InFrequency)
log.Infof("Out Volt: %.2fV Out Cur: %.2fA Out Freq %.2fHz", info.OutVoltage, info.OutCurrent, info.OutFrequency)
log.Infof("In Power %.2fW Out Power %.2fW", info.InVoltage*info.InCurrent, info.OutVoltage*info.OutCurrent)
log.Infof("Charge State: %.2f%%", info.ChargeState*100)
log.Info("LEDs state:")
for k, v := range info.LEDs {
out += fmt.Sprintf(" %s %s", mk2driver.LedNames[k], mk2driver.StateNames[v])
log.Infof(" %s %s", mk2driver.LedNames[k], mk2driver.StateNames[v])
}
out += "\nErrors:"
for _, v := range info.Errors {
out += " " + v.Error()
if len(info.Errors) != 0 {
log.Info("Errors:")
for _, err := range info.Errors {
log.Error(err)
}
}
out += "\n"
log.Printf("System Info: \n%v", out)
}

View File

@@ -2,13 +2,15 @@ package mqttclient
import (
"encoding/json"
"fmt"
"time"
"github.com/diebietse/invertergui/mk2driver"
mqtt "github.com/eclipse/paho.mqtt.golang"
"github.com/sirupsen/logrus"
)
var log = logrus.WithField("ctx", "inverter-gui-mqtt")
const keepAlive = 5 * time.Second
// Config sets MQTT client configuration
@@ -32,14 +34,14 @@ func New(mk2 mk2driver.Mk2, config Config) error {
if e.Valid {
data, err := json.Marshal(e)
if err != nil {
fmt.Printf("Data error: %v\n", err)
log.Errorf("Could not parse data source: %v", err)
continue
}
t := c.Publish(config.Topic, 0, false, data)
t.Wait()
if t.Error() != nil {
fmt.Printf("Error: %v\n", t.Error())
log.Errorf("Could not publish data: %v", t.Error())
}
}
}
@@ -61,10 +63,10 @@ func getOpts(config Config) *mqtt.ClientOptions {
opts.SetKeepAlive(keepAlive)
opts.SetOnConnectHandler(func(mqtt.Client) {
fmt.Print("Client connected to broker")
log.Info("Client connected to broker")
})
opts.SetConnectionLostHandler(func(cli mqtt.Client, err error) {
fmt.Printf("Client connection to broker losted: %v", err)
log.Errorf("Client connection to broker lost: %v", err)
})
return opts

View File

@@ -37,8 +37,11 @@ import (
"time"
"github.com/diebietse/invertergui/mk2driver"
"github.com/sirupsen/logrus"
)
var log = logrus.WithField("ctx", "inverter-gui-munin")
type Munin struct {
mk2driver.Mk2
muninResponse chan *muninData
@@ -63,6 +66,7 @@ func NewMunin(mk2 mk2driver.Mk2) *Munin {
func (m *Munin) ServeMuninHTTP(rw http.ResponseWriter, r *http.Request) {
muninDat := <-m.muninResponse
if muninDat.timesUpdated == 0 {
log.Error("No data returned")
rw.WriteHeader(500)
_, _ = rw.Write([]byte("No data to return.\n"))
return
@@ -95,7 +99,7 @@ func (m *Munin) ServeMuninHTTP(rw http.ResponseWriter, r *http.Request) {
_, err := rw.Write(outputBuf.Bytes())
if err != nil {
fmt.Printf("%v\n", err)
log.Errorf("Could not write data response: %v", err)
}
}
@@ -103,7 +107,7 @@ func (m *Munin) ServeMuninConfigHTTP(rw http.ResponseWriter, r *http.Request) {
output := muninConfig
_, err := rw.Write([]byte(output))
if err != nil {
fmt.Printf("%v\n", err)
log.Errorf("Could not write config response: %v", err)
}
}

View File

@@ -32,15 +32,17 @@ package webui
import (
"fmt"
"log"
"net/http"
"sync"
"time"
"github.com/diebietse/invertergui/mk2driver"
"github.com/diebietse/invertergui/websocket"
"github.com/sirupsen/logrus"
)
var log = logrus.WithField("ctx", "inverter-gui-webgui")
const (
LedOff = "dot-off"
LedRed = "dot-red"
@@ -169,7 +171,7 @@ func (w *WebGui) dataPoll() {
if s.Valid {
err := w.hub.Broadcast(buildTemplateInput(s))
if err != nil {
log.Printf("Could not send update to clients: %v", err)
log.Errorf("Could not send update to clients: %v", err)
}
}
case <-w.stopChan: