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

View File

@ -3,7 +3,6 @@ package device
import ( import (
"context" "context"
"fmt" "fmt"
"os"
"git.quimbo.fr/nicolas/mqtt" "git.quimbo.fr/nicolas/mqtt"
"github.com/rs/zerolog" "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(ctx context.Context) {
// func (m *DeviceManager) CheckAll() { logger := zerolog.Ctx(ctx)
logger := zerolog.New(os.Stdout)
for _, device := range m.devices { 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("") logger.Error().Err(err).Msg("")
} }
} }