From 561419b568e319bb4b79f48186d077761ad8f51a Mon Sep 17 00:00:00 2001 From: Sebastian Mark Date: Sun, 3 Nov 2024 22:22:03 +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/client/app.go | 3 +-- internal/helper/files.go | 5 ++--- internal/helper/logging.go | 12 ++++++++++++ internal/websocket/connect.go | 6 +++--- internal/websocket/receive.go | 6 +++--- internal/websocket/send.go | 6 +++--- 6 files changed, 24 insertions(+), 14 deletions(-) create mode 100644 internal/helper/logging.go diff --git a/cmd/client/app.go b/cmd/client/app.go index bb5e280..1fb7738 100644 --- a/cmd/client/app.go +++ b/cmd/client/app.go @@ -2,7 +2,6 @@ package client import ( "flag" - "github.com/charmbracelet/log" "git.smsvc.net/pomodoro/ChronoTomato/internal/helper" "git.smsvc.net/pomodoro/ChronoTomato/internal/websocket" @@ -87,6 +86,6 @@ func Start() { _, err := tea.NewProgram(myApp()).Run() if err != nil { - log.Fatal("Could not start program:", "msg", err) + helper.Logger.Fatal("Could not start program:", "msg", err) } } diff --git a/internal/helper/files.go b/internal/helper/files.go index 230fd93..3187151 100644 --- a/internal/helper/files.go +++ b/internal/helper/files.go @@ -1,7 +1,6 @@ package helper import ( - "github.com/charmbracelet/log" "gopkg.in/yaml.v3" "os" "path/filepath" @@ -33,12 +32,12 @@ func ParseConfig(filename string) ChronoTomato.Config { yamlFile, err := os.ReadFile(expandUnixPath(filename)) if err != nil { - log.Fatal("Error opening config file!", "reason", err) + Logger.Fatal("Error opening config file!", "reason", err) } err = yaml.Unmarshal(yamlFile, &config) if err != nil { - log.Fatalf("Unmarshal: %v", err) + Logger.Fatalf("Unmarshal: %v", err) } return config 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/connect.go b/internal/websocket/connect.go index fdbc55d..770cd13 100644 --- a/internal/websocket/connect.go +++ b/internal/websocket/connect.go @@ -1,10 +1,10 @@ package websocket import ( - "github.com/charmbracelet/log" "github.com/gorilla/websocket" "time" + "git.smsvc.net/pomodoro/ChronoTomato/internal/helper" ChronoTomato "git.smsvc.net/pomodoro/ChronoTomato/pkg/models" ) @@ -14,10 +14,10 @@ type Client ChronoTomato.GoTomatoClient // New websocket client func Connect(url string) Client { conn, _, err := websocket.DefaultDialer.Dial(url, nil) if err != nil { - log.Fatal("Dial error!", "reason", err) + helper.Logger.Fatal("Dial error!", "reason", err) } - log.Info("Connected 󰖟 ", "host", url) + helper.Logger.Info("Connected 󰖟 ", "host", url) time.Sleep(time.Second) return Client{Conn: conn} diff --git a/internal/websocket/receive.go b/internal/websocket/receive.go index c21252b..bb36722 100644 --- a/internal/websocket/receive.go +++ b/internal/websocket/receive.go @@ -2,9 +2,9 @@ package websocket import ( "encoding/json" - "github.com/charmbracelet/log" "github.com/gorilla/websocket" + "git.smsvc.net/pomodoro/ChronoTomato/internal/helper" GoTomato "git.smsvc.net/pomodoro/GoTomato/pkg/models" ) @@ -25,14 +25,14 @@ func (c Client) ProcessServerMessages(channel chan<- GoTomato.ServerMessage) { return } // Log any other errors - log.Error("Read error!", "reason", err) + helper.Logger.Error("Read error!", "reason", err) close(channel) return } err = json.Unmarshal(message, &serverMessage) if err != nil { - log.Error("Error unmarshalling!", "reason", err) + helper.Logger.Error("Error unmarshalling!", "reason", err) continue } diff --git a/internal/websocket/send.go b/internal/websocket/send.go index c3871b0..a3126cc 100644 --- a/internal/websocket/send.go +++ b/internal/websocket/send.go @@ -2,9 +2,9 @@ package websocket import ( "encoding/json" - "github.com/charmbracelet/log" "github.com/gorilla/websocket" + "git.smsvc.net/pomodoro/ChronoTomato/internal/helper" GoTomato "git.smsvc.net/pomodoro/GoTomato/pkg/models" ) @@ -12,13 +12,13 @@ import ( func sendClientCommand(c Client, msg GoTomato.ClientCommand) { messageBytes, err := json.Marshal(msg) if err != nil { - log.Error("Error marshalling!", "reason", err) + helper.Logger.Error("Error marshalling!", "reason", err) return } err = c.Conn.WriteMessage(websocket.TextMessage, messageBytes) if err != nil { - log.Error("Write error!", "reason", err) + helper.Logger.Error("Write error!", "reason", err) return } }