Standardize logging across codebase with consistent structured format

This commit is contained in:
2025-05-22 06:54:30 -07:00
parent 634cc45f3a
commit b8f2ea7b7b
4 changed files with 41 additions and 22 deletions

24
main.go
View File

@@ -3,7 +3,6 @@ package main
import (
"database/sql"
"flag"
"fmt"
"log"
"os"
"os/signal"
@@ -25,7 +24,9 @@ var (
)
func main() {
fmt.Fprintf(os.Stderr, "[%s] starting gomeshalerter\n", runStart.Format("15:04:05"))
logInfo("main", "Starting gomeshalerter", map[string]interface{}{
"timestamp": runStart.Format("15:04:05"),
})
setupLogging()
defer flushLog()
@@ -37,12 +38,14 @@ func main() {
// Define a cleanup function to properly close resources
cleanup := func() {
fmt.Fprintf(os.Stderr, "[shutdown] Closing database...\n")
logInfo("shutdown", "Closing database", nil)
if err := db.Close(); err != nil {
fmt.Fprintf(os.Stderr, "[shutdown] Error closing database: %v\n", err)
logInfo("shutdown", "Error closing database", map[string]interface{}{
"error": err.Error(),
})
}
flushLog()
fmt.Fprintf(os.Stderr, "[shutdown] Cleanup complete\n")
logInfo("shutdown", "Cleanup complete", nil)
}
// Ensure cleanup runs on normal exit
defer cleanup()
@@ -57,7 +60,10 @@ func main() {
ollamaURL = "http://localhost:11434" // Default Ollama server URL
}
fmt.Fprintf(os.Stderr, "[ollama] Using model: %s at %s\n", ollamaModel, ollamaURL)
logInfo("ollama", "Using model", map[string]interface{}{
"model": ollamaModel,
"url": ollamaURL,
})
// Replace --broadcast flag with --dry-run flag (default is to broadcast)
dryRun := flag.Bool("dry-run", false, "don't actually send to meshtastic, just print what would be sent")
@@ -75,9 +81,9 @@ func main() {
go func() {
<-sigChan
fmt.Fprintf(os.Stderr, "[shutdown] Received signal, performing cleanup before exit...\n")
logInfo("shutdown", "Received signal, performing cleanup before exit", nil)
cleanup()
fmt.Fprintf(os.Stderr, "[shutdown] Exiting...\n")
logInfo("shutdown", "Exiting", nil)
os.Exit(0) // Exit after cleanup
}()
@@ -118,5 +124,5 @@ func main() {
// Wait for all goroutines to finish
wg.Wait()
fmt.Fprintf(os.Stderr, "[shutdown] All goroutines stopped, exiting...\n")
logInfo("shutdown", "All goroutines stopped, exiting", nil)
}