Improve logging

This commit is contained in:
Nicolas Duhamel 2022-12-11 15:11:58 +01:00
parent 77fd085e9a
commit f5e9d460c8
2 changed files with 11 additions and 13 deletions

18
main.go
View File

@ -24,7 +24,7 @@ type Config struct {
Port string
Username string
Password string
Clientid string
ClientId string
}
type App struct {
@ -41,7 +41,7 @@ func NewApp(conf Config) *App {
Port: conf.Port,
Username: conf.Username,
Password: conf.Password,
ClientID: conf.Clientid,
ClientID: conf.ClientId,
CleanSession: true,
AutoReconnect: true,
Retained: false,
@ -92,19 +92,19 @@ func (app *App) subscribe(
func (app *App) runTicker() {
ticker := time.NewTicker(60 * time.Second)
// logger := zerolog.Ctx(app.ctx).With().Str("action", "ticker").Logger()
// logger.Info().Msg("Start ticker")
logger := zerolog.Ctx(app.ctx).With().Str("action", "ticker").Logger()
logger.Info().Msg("Start ticker")
// ctx := logger.WithContext(app.ctx)
go func() {
for {
select {
case <-app.ctx.Done():
return
// case t := <-ticker.C:
case <-ticker.C:
case t := <-ticker.C:
// case <-ticker.C:
ctx, cancel := context.WithCancel(app.ctx)
// logger.Info().Time("at", t).Msg("Tick")
ctx = logger.WithContext(app.ctx)
logger.Info().Time("at", t).Msg("Tick")
app.DeviceManager.CheckAll(ctx)
cancel()
}
@ -265,7 +265,7 @@ func main() {
Port: *port,
Username: *username,
Password: *password,
Clientid: *clientid,
ClientId: *clientid,
}
app := NewApp(config)

View File

@ -3,7 +3,6 @@ package device
import (
"context"
"fmt"
"os"
"git.quimbo.fr/nicolas/mqtt"
"github.com/rs/zerolog"
@ -88,11 +87,10 @@ func (m *DeviceManager) Check(ctx context.Context, name string) error {
}
func (m *DeviceManager) CheckAll(ctx context.Context) {
// func (m *DeviceManager) CheckAll() {
logger := zerolog.New(os.Stdout)
logger := zerolog.Ctx(ctx)
for _, device := range m.devices {
if err := device.CheckSetpoint(logger, m.PubChan); err != nil {
if err := device.CheckSetpoint(*logger, m.PubChan); err != nil {
logger.Error().Err(err).Msg("")
}
}