Add more logger code.
All checks were successful
Build Docker image / build (push) Successful in 53s

This commit is contained in:
Pieter Hollander 2024-02-20 16:19:01 +01:00
parent f5fe3f5080
commit 4aa065ee82
Signed by: pieter
SSH key fingerprint: SHA256:HbX+9cBXsop9SuvL+mELd29sK+7DehFfdVweFVDtMSg

27
main.go
View file

@ -73,6 +73,7 @@ type Payload struct {
}
var db *sql.DB
var logger *slog.Logger
func main() {
// Setup slog
@ -81,7 +82,7 @@ func main() {
// Load environment variables from .env file if it exists
if err := godotenv.Load(); err != nil {
logger.Info("No .env file found or error loading .env file", "error", err)
logger.Info("No .env file found or error loading .env file (ignore this message when using container)", "error", err)
}
// Connect to PostgreSQL
@ -97,13 +98,13 @@ func main() {
opts.SetPassword(os.Getenv("MQTT_PASSWORD"))
opts.SetAutoReconnect(true)
opts.SetConnectionLostHandler(func(client mqtt.Client, err error) {
log.Printf("Connection lost: %v", err)
logger.Error("Connection lost", "error", err)
})
opts.SetOnConnectHandler(func(client mqtt.Client) {
topic := os.Getenv("MQTT_TOPIC")
log.Println("Connected to MQTT broker, subscribing to topic...")
logger.Info("Connected to MQTT broker, subscribing to topic...", "topic", topic)
if token := client.Subscribe(topic, 0, mqttMessageHandler); token.Wait() && token.Error() != nil {
log.Printf("Error subscribing to MQTT topic %s: %v", topic, token.Error())
logger.Error("Error subscribing to MQTT topic", "topic", topic, "error", token.Error())
}
})
@ -144,14 +145,14 @@ func mqttMessageHandler(client mqtt.Client, msg mqtt.Message) {
var payload Payload
err := json.Unmarshal(msg.Payload(), &payload)
if err != nil {
log.Println("Error parsing MQTT payload:", err)
logger.Error("Error parsing MQTT payload", "error", err)
return
}
// Parse timestamp to time.Time
timestamp, err := parseTimestamp(payload.T)
if err != nil {
log.Println("Error parsing timestamp:", err)
logger.Error("Error parsing timestamp", "error", err)
return
}
@ -182,15 +183,17 @@ func mqttMessageHandler(client mqtt.Client, msg mqtt.Message) {
// If any value has changed, log all the relevant values
if changed {
log.Printf("Values changed: dt1=%s, dt2=%s, rt1=%s, rt2=%s, g=%s\n",
safeDerefInt(payload.Dt1), safeDerefInt(payload.Dt2),
safeDerefInt(payload.Rt1), safeDerefInt(payload.Rt2),
safeDerefInt(payload.G))
logger.Debug("Values changed",
"dt1", safeDerefInt(payload.Dt1),
"dt2", safeDerefInt(payload.Dt2),
"rt1", safeDerefInt(payload.Rt1),
"rt2", safeDerefInt(payload.Rt2),
"g", safeDerefInt(payload.G))
}
// Insert data into PostgreSQL
err = insertData(timestamp, payload)
if err != nil {
log.Println("Error inserting data into PostgreSQL:", err)
logger.Error("Error inserting data into PostgreSQL", "error", err)
}
}
@ -248,7 +251,7 @@ func connectToPostgreSQL(pgConnStr string) error {
break // Successfully connected
}
log.Println("Error connecting to PostgreSQL:", err)
logger.Error("Error connecting to PostgreSQL", "error", err)
time.Sleep(5 * time.Second) // Retry after 5 seconds
}