From 600d2ed2ffffc69d5832648fbfaf544dc1774a4e Mon Sep 17 00:00:00 2001 From: Sebastian Mark Date: Mon, 4 Nov 2024 20:33:25 +0100 Subject: [PATCH] feat: implement centralized logging helper MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - remove direct log usage - create a new logging helper - update all log calls to use the new Logger instance - set timestamp to ISO8601 🤖 --- cmd/server/main.go | 8 ++++---- internal/helper/logging.go | 12 ++++++++++++ internal/websocket/broadcast.go | 6 +++--- internal/websocket/client_commands.go | 10 +++++----- internal/websocket/handle_connection.go | 6 +++--- pkg/models/client.go | 5 +++-- 6 files changed, 30 insertions(+), 17 deletions(-) create mode 100644 internal/helper/logging.go diff --git a/cmd/server/main.go b/cmd/server/main.go index c30f3e4..a109f38 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -3,11 +3,11 @@ package server import ( "flag" "fmt" - "github.com/charmbracelet/log" "github.com/gorilla/handlers" "net/http" "os" + "git.smsvc.net/pomodoro/GoTomato/internal/helper" "git.smsvc.net/pomodoro/GoTomato/internal/metadata" "git.smsvc.net/pomodoro/GoTomato/internal/shared" "git.smsvc.net/pomodoro/GoTomato/internal/websocket" @@ -48,12 +48,12 @@ func Start() { r.HandleFunc("/ws", websocket.HandleConnection) go websocket.SendPermanentBroadCastMessage() - log.Info("GoTomato started", "version", metadata.GoTomatoVersion) - log.Info("Websocket listening", "address", listen) + helper.Logger.Info("GoTomato started", "version", metadata.GoTomatoVersion) + helper.Logger.Info("Websocket listening", "address", listen) // start the listener err := http.ListenAndServe(listen, handlers.ProxyHeaders(r)) if err != nil { - log.Fatal("Error starting server:", "msg", err) + helper.Logger.Fatal("Error starting server:", "msg", err) } } diff --git a/internal/helper/logging.go b/internal/helper/logging.go new file mode 100644 index 0000000..ecb7371 --- /dev/null +++ b/internal/helper/logging.go @@ -0,0 +1,12 @@ +package helper + +import ( + "github.com/charmbracelet/log" + "os" + "time" +) + +var Logger = log.NewWithOptions(os.Stdout, log.Options{ + ReportTimestamp: true, + TimeFormat: time.RFC3339, +}) diff --git a/internal/websocket/broadcast.go b/internal/websocket/broadcast.go index 14254c0..def8309 100644 --- a/internal/websocket/broadcast.go +++ b/internal/websocket/broadcast.go @@ -2,10 +2,10 @@ package websocket import ( "encoding/json" - "github.com/charmbracelet/log" "github.com/gorilla/websocket" "time" + "git.smsvc.net/pomodoro/GoTomato/internal/helper" "git.smsvc.net/pomodoro/GoTomato/internal/shared" ) @@ -16,14 +16,14 @@ func SendPermanentBroadCastMessage() { // Marshal the message into JSON format jsonMessage, err := json.Marshal(shared.State) if err != nil { - log.Error("Error marshalling message:", "msg", err) + helper.Logger.Error("Error marshalling message:", "msg", err) return } // Iterate over all connected clients and broadcast the message for _, client := range Clients { err := client.SendMessage(websocket.TextMessage, jsonMessage) if err != nil { - log.Error("Error broadcasting to client:", "msg", err) + helper.Logger.Error("Error broadcasting to client:", "msg", err) // The client is responsible for closing itself on error } } diff --git a/internal/websocket/client_commands.go b/internal/websocket/client_commands.go index 1a66198..bdb6e7a 100644 --- a/internal/websocket/client_commands.go +++ b/internal/websocket/client_commands.go @@ -2,8 +2,8 @@ package websocket import ( "encoding/json" - "github.com/charmbracelet/log" + "git.smsvc.net/pomodoro/GoTomato/internal/helper" "git.smsvc.net/pomodoro/GoTomato/internal/pomodoro" "git.smsvc.net/pomodoro/GoTomato/internal/shared" "git.smsvc.net/pomodoro/GoTomato/pkg/models" @@ -22,14 +22,14 @@ func handleClientCommands(c models.WebsocketClient) { _, message, err := ws.ReadMessage() if err != nil { delete(Clients, ws.LocalAddr()) - log.Info("Client disconnected:", "msg", err, "host", c.RealIP, "clients", len(Clients)) + helper.Logger.Info("Client disconnected:", "msg", err, "host", c.RealIP, "clients", len(Clients)) break } // Handle incoming commands err = json.Unmarshal(message, &clientCommand) if err != nil { - log.Error("Error unmarshalling command:", "msg", err) + helper.Logger.Error("Error unmarshalling command:", "msg", err) continue } @@ -55,10 +55,10 @@ func handleClientCommands(c models.WebsocketClient) { case "updateSettings": if !pomodoro.IsPomodoroOngoing() { if !checkSettings(clientCommand.Settings) { - log.Warn("Ignoring invalid config:", "msg", clientCommand.Settings, "host", c.Conn.RemoteAddr()) + helper.Logger.Warn("Ignoring invalid config:", "msg", clientCommand.Settings, "host", c.Conn.RemoteAddr()) break } - log.Info("Client send config", "config", clientCommand.Settings, "host", c.Conn.RemoteAddr()) + helper.Logger.Info("Client send config", "config", clientCommand.Settings, "host", c.Conn.RemoteAddr()) pomodoro.UpdateSettings(clientCommand.Settings) } } diff --git a/internal/websocket/handle_connection.go b/internal/websocket/handle_connection.go index 6ee22bc..9c4b43f 100644 --- a/internal/websocket/handle_connection.go +++ b/internal/websocket/handle_connection.go @@ -1,12 +1,12 @@ package websocket import ( - "github.com/charmbracelet/log" "github.com/gorilla/websocket" "net" "net/http" "sync" + "git.smsvc.net/pomodoro/GoTomato/internal/helper" "git.smsvc.net/pomodoro/GoTomato/pkg/models" ) @@ -24,7 +24,7 @@ func HandleConnection(w http.ResponseWriter, r *http.Request) { // Upgrade initial GET request to a WebSocket ws, err := upgrader.Upgrade(w, r, nil) if err != nil { - log.Error("WebSocket upgrade error:", "msg", err) + helper.Logger.Error("WebSocket upgrade error:", "msg", err) return } defer ws.Close() @@ -38,7 +38,7 @@ func HandleConnection(w http.ResponseWriter, r *http.Request) { Clients[ws.LocalAddr()] = &client mu.Unlock() - log.Info("Client connected", "host", client.RealIP, "clients", len(Clients)) + helper.Logger.Info("Client connected", "host", client.RealIP, "clients", len(Clients)) // Listen for commands from the connected client handleClientCommands(client) diff --git a/pkg/models/client.go b/pkg/models/client.go index aec2f3c..eef9663 100644 --- a/pkg/models/client.go +++ b/pkg/models/client.go @@ -1,8 +1,9 @@ package models import ( - "github.com/charmbracelet/log" "github.com/gorilla/websocket" + + "git.smsvc.net/pomodoro/GoTomato/internal/helper" ) // Represents a command from the client (start/stop) @@ -23,7 +24,7 @@ type WebsocketClient struct { func (c *WebsocketClient) SendMessage(messageType int, data []byte) error { err := c.Conn.WriteMessage(messageType, data) if err != nil { - log.Error("Error writing to WebSocket:", "msg", err) + helper.Logger.Error("Error writing to WebSocket:", "msg", err) c.Conn.Close() // Close the connection on error } return err