From 899417b605bdb1d0b9d47121d45272d7d062870e Mon Sep 17 00:00:00 2001 From: Sebastian Mark Date: Sat, 26 Oct 2024 00:01:07 +0200 Subject: [PATCH] feat: replace standard log with charmbracelet/log MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - update logging to use charmbracelet/log package - improve log messages for server start and errors - enhance client connection and disconnection logs 🤖 --- cmd/server/main.go | 6 ++-- go.mod | 19 ++++++++++++- go.sum | 36 ++++++++++++++++++++++++ internal/websocket/broadcast.go | 6 ++-- internal/websocket/client_commands.go | 6 ++-- internal/websocket/handle_connections.go | 6 ++-- pkg/models/client.go | 4 +-- 7 files changed, 68 insertions(+), 15 deletions(-) diff --git a/cmd/server/main.go b/cmd/server/main.go index 828602b..e715843 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -7,7 +7,7 @@ import ( "git.smsvc.net/pomodoro/GoTomato/internal/shared" "git.smsvc.net/pomodoro/GoTomato/internal/websocket" "git.smsvc.net/pomodoro/GoTomato/pkg/models" - "log" + "github.com/charmbracelet/log" "net/http" "os" "strings" @@ -39,9 +39,9 @@ func Start() { http.HandleFunc("/ws", websocket.HandleConnections) go websocket.SendPermanentBroadCastMessage() - log.Printf("Pomodoro WebSocket server started on %s\n", listen) + log.Info("Pomodoro WebSocket server started!", "listen", listen) err := http.ListenAndServe(listen, nil) if err != nil { - log.Fatalf("Error starting server: %v", err) + log.Fatal("Error starting server:", "msg", err) } } diff --git a/go.mod b/go.mod index cc88ebb..148683e 100644 --- a/go.mod +++ b/go.mod @@ -2,4 +2,21 @@ module git.smsvc.net/pomodoro/GoTomato go 1.23 -require github.com/gorilla/websocket v1.5.3 +require ( + github.com/charmbracelet/log v0.4.0 + github.com/gorilla/websocket v1.5.3 +) + +require ( + github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect + github.com/charmbracelet/lipgloss v0.10.0 // indirect + github.com/go-logfmt/logfmt v0.6.0 // indirect + github.com/lucasb-eyer/go-colorful v1.2.0 // indirect + github.com/mattn/go-isatty v0.0.18 // indirect + github.com/mattn/go-runewidth v0.0.15 // indirect + github.com/muesli/reflow v0.3.0 // indirect + github.com/muesli/termenv v0.15.2 // indirect + github.com/rivo/uniseg v0.4.7 // indirect + golang.org/x/exp v0.0.0-20231006140011-7918f672742d // indirect + golang.org/x/sys v0.13.0 // indirect +) diff --git a/go.sum b/go.sum index 25a9fc4..9437970 100644 --- a/go.sum +++ b/go.sum @@ -1,2 +1,38 @@ +github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiErDT4WkJ2k= +github.com/aymanbagabas/go-osc52/v2 v2.0.1/go.mod h1:uYgXzlJ7ZpABp8OJ+exZzJJhRNQ2ASbcXHWsFqH8hp8= +github.com/charmbracelet/lipgloss v0.10.0 h1:KWeXFSexGcfahHX+54URiZGkBFazf70JNMtwg/AFW3s= +github.com/charmbracelet/lipgloss v0.10.0/go.mod h1:Wig9DSfvANsxqkRsqj6x87irdy123SR4dOXlKa91ciE= +github.com/charmbracelet/log v0.4.0 h1:G9bQAcx8rWA2T3pWvx7YtPTPwgqpk7D68BX21IRW8ZM= +github.com/charmbracelet/log v0.4.0/go.mod h1:63bXt/djrizTec0l11H20t8FDSvA4CRZJ1KH22MdptM= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/go-logfmt/logfmt v0.6.0 h1:wGYYu3uicYdqXVgoYbvnkrPVXkuLM1p1ifugDMEdRi4= +github.com/go-logfmt/logfmt v0.6.0/go.mod h1:WYhtIu8zTZfxdn5+rREduYbwxfcBr/Vr6KEVveWlfTs= github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg= github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= +github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY= +github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0= +github.com/mattn/go-isatty v0.0.18 h1:DOKFKCQ7FNG2L1rbrmstDN4QVRdS89Nkh85u68Uwp98= +github.com/mattn/go-isatty v0.0.18/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= +github.com/mattn/go-runewidth v0.0.12/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk= +github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U= +github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= +github.com/muesli/reflow v0.3.0 h1:IFsN6K9NfGtjeggFP+68I4chLZV2yIKsXJFNZ+eWh6s= +github.com/muesli/reflow v0.3.0/go.mod h1:pbwTDkVPibjO2kyvBQRBxTWEEGDGq0FlB1BIKtnHY/8= +github.com/muesli/termenv v0.15.2 h1:GohcuySI0QmI3wN8Ok9PtKGkgkFIk7y6Vpb5PvrY+Wo= +github.com/muesli/termenv v0.15.2/go.mod h1:Epx+iuz8sNs7mNKhxzH4fWXGNpZwUaJKRS1noLXviQ8= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= +github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= +github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ= +github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88= +github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= +github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +golang.org/x/exp v0.0.0-20231006140011-7918f672742d h1:jtJma62tbqLibJ5sFQz8bKtEM8rJBtfilJ2qTU199MI= +golang.org/x/exp v0.0.0-20231006140011-7918f672742d/go.mod h1:ldy0pHrwJyGW56pPQzzkH36rKxoZW1tw7ZJpeKx+hdo= +golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE= +golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/internal/websocket/broadcast.go b/internal/websocket/broadcast.go index 2fd5754..99827e7 100644 --- a/internal/websocket/broadcast.go +++ b/internal/websocket/broadcast.go @@ -3,8 +3,8 @@ package websocket import ( "encoding/json" "git.smsvc.net/pomodoro/GoTomato/internal/shared" + "github.com/charmbracelet/log" "github.com/gorilla/websocket" - "log" "time" ) @@ -15,14 +15,14 @@ func SendPermanentBroadCastMessage() { // Marshal the message into JSON format jsonMessage, err := json.Marshal(shared.Message) if err != nil { - log.Printf("Error marshalling message: %v", err) + log.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.Printf("Error broadcasting to client: %v", err) + log.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 d394601..4a0cbd1 100644 --- a/internal/websocket/client_commands.go +++ b/internal/websocket/client_commands.go @@ -5,8 +5,8 @@ import ( "git.smsvc.net/pomodoro/GoTomato/internal/pomodoro" "git.smsvc.net/pomodoro/GoTomato/internal/shared" "git.smsvc.net/pomodoro/GoTomato/pkg/models" + "github.com/charmbracelet/log" "github.com/gorilla/websocket" - "log" ) // handleClientCommands listens for commands from WebSocket clients @@ -16,7 +16,7 @@ func handleClientCommands(ws *websocket.Conn) { _, message, err := ws.ReadMessage() if err != nil { - log.Printf("Client disconnected from %s (%v) (total: %d)", ws.NetConn().RemoteAddr(), err, len(Clients)-1) + log.Info("Client disconnected:", "msg", err, "host", ws.NetConn().RemoteAddr(), "clients", len(Clients)-1) delete(Clients, ws) break } @@ -24,7 +24,7 @@ func handleClientCommands(ws *websocket.Conn) { // Handle incoming commands err = json.Unmarshal(message, &clientCommand) if err != nil { - log.Printf("Error unmarshalling command: %v", err) + log.Error("Error unmarshalling command:", "msg", err) continue } diff --git a/internal/websocket/handle_connections.go b/internal/websocket/handle_connections.go index 47c1e9a..687f472 100644 --- a/internal/websocket/handle_connections.go +++ b/internal/websocket/handle_connections.go @@ -2,8 +2,8 @@ package websocket import ( "git.smsvc.net/pomodoro/GoTomato/pkg/models" + "github.com/charmbracelet/log" "github.com/gorilla/websocket" - "log" "net/http" "sync" ) @@ -22,12 +22,12 @@ func HandleConnections(w http.ResponseWriter, r *http.Request) { // Upgrade initial GET request to a WebSocket ws, err := upgrader.Upgrade(w, r, nil) if err != nil { - log.Printf("WebSocket upgrade error: %v", err) + log.Error("WebSocket upgrade error:", "msg", err) return } defer ws.Close() - log.Printf("Client connected from %v (total: %d)", ws.NetConn().RemoteAddr(), len(Clients)+1) + log.Info("Client connected", "host", ws.NetConn().RemoteAddr(), "clients", len(Clients)+1) // Register the new client mu.Lock() diff --git a/pkg/models/client.go b/pkg/models/client.go index 95d8d44..6862cb1 100644 --- a/pkg/models/client.go +++ b/pkg/models/client.go @@ -1,8 +1,8 @@ package models import ( + "github.com/charmbracelet/log" "github.com/gorilla/websocket" - "log" "sync" ) @@ -25,7 +25,7 @@ func (c *Client) SendMessage(messageType int, data []byte) error { err := c.Conn.WriteMessage(messageType, data) if err != nil { - log.Printf("Error writing to WebSocket: %v", err) + log.Error("Error writing to WebSocket:", "msg", err) c.Conn.Close() // Close the connection on error } return err