Introduce log/slog to add log levels.
All checks were successful
Build Docker image / build (push) Successful in 55s

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

27
main.go
View file

@ -5,6 +5,7 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"log" "log"
"log/slog"
"os" "os"
"time" "time"
_ "time/tzdata" _ "time/tzdata"
@ -74,25 +75,21 @@ type Payload struct {
var db *sql.DB var db *sql.DB
func main() { func main() {
// Setup slog
logger := slog.New(slog.NewJSONHandler(os.Stdout, nil))
slog.SetDefault(logger)
// Load environment variables from .env file if it exists // Load environment variables from .env file if it exists
if err := godotenv.Load(); err != nil { if err := godotenv.Load(); err != nil {
log.Println("No .env file found or error loading .env file") logger.Info("No .env file found or error loading .env file", "error", err)
} }
// Connect to PostgreSQL // Connect to PostgreSQL
pgConnStr := os.Getenv("PG_DB") pgConnStr := os.Getenv("PG_DB")
if err := connectToPostgreSQL(pgConnStr); err != nil { if err := connectToPostgreSQL(pgConnStr); err != nil {
log.Fatal("Error connecting to PostgreSQL:", err) log.Fatal("Error connecting to PostgreSQL", "error", err)
} }
// pgConnStr := os.Getenv("PG_DB")
// var err error
// db, err = sql.Open("postgres", pgConnStr)
// if err != nil {
// log.Fatal(err)
// }
// defer db.Close()
// Initialize MQTT options // Initialize MQTT options
opts := mqtt.NewClientOptions() opts := mqtt.NewClientOptions()
opts.AddBroker(os.Getenv("MQTT_BROKER")) opts.AddBroker(os.Getenv("MQTT_BROKER"))
@ -122,16 +119,6 @@ func main() {
var prevDt1, prevDt2, prevRt1, prevRt2, prevG int var prevDt1, prevDt2, prevRt1, prevRt2, prevG int
// func updateFieldIfChanged(currentValue *int, previousValue *int) *int {
// if currentValue != nil && *currentValue == *previousValue {
// return nil // No change, so we prepare to insert a NULL
// } else if currentValue != nil {
// *previousValue = *currentValue // Update the previous value to the current one
// return currentValue
// }
// return currentValue // Return the original value if it's nil
// }
func updateFieldIfChanged(currentValue *int, previousValue *int) (*int, bool) { func updateFieldIfChanged(currentValue *int, previousValue *int) (*int, bool) {
if currentValue != nil && *currentValue == *previousValue { if currentValue != nil && *currentValue == *previousValue {
return nil, false // No change return nil, false // No change