feat: implement centralized logging helper

- remove direct log usage
- create a new logging helper
- update all log calls to use the new Logger instance
- set timestamp to ISO8601

🤖
This commit is contained in:
Sebastian Mark 2024-11-03 22:22:03 +01:00
parent ebc81657f5
commit 561419b568
6 changed files with 24 additions and 14 deletions

View file

@ -2,7 +2,6 @@ package client
import ( import (
"flag" "flag"
"github.com/charmbracelet/log"
"git.smsvc.net/pomodoro/ChronoTomato/internal/helper" "git.smsvc.net/pomodoro/ChronoTomato/internal/helper"
"git.smsvc.net/pomodoro/ChronoTomato/internal/websocket" "git.smsvc.net/pomodoro/ChronoTomato/internal/websocket"
@ -87,6 +86,6 @@ func Start() {
_, err := tea.NewProgram(myApp()).Run() _, err := tea.NewProgram(myApp()).Run()
if err != nil { if err != nil {
log.Fatal("Could not start program:", "msg", err) helper.Logger.Fatal("Could not start program:", "msg", err)
} }
} }

View file

@ -1,7 +1,6 @@
package helper package helper
import ( import (
"github.com/charmbracelet/log"
"gopkg.in/yaml.v3" "gopkg.in/yaml.v3"
"os" "os"
"path/filepath" "path/filepath"
@ -33,12 +32,12 @@ func ParseConfig(filename string) ChronoTomato.Config {
yamlFile, err := os.ReadFile(expandUnixPath(filename)) yamlFile, err := os.ReadFile(expandUnixPath(filename))
if err != nil { if err != nil {
log.Fatal("Error opening config file!", "reason", err) Logger.Fatal("Error opening config file!", "reason", err)
} }
err = yaml.Unmarshal(yamlFile, &config) err = yaml.Unmarshal(yamlFile, &config)
if err != nil { if err != nil {
log.Fatalf("Unmarshal: %v", err) Logger.Fatalf("Unmarshal: %v", err)
} }
return config return config

View file

@ -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,
})

View file

@ -1,10 +1,10 @@
package websocket package websocket
import ( import (
"github.com/charmbracelet/log"
"github.com/gorilla/websocket" "github.com/gorilla/websocket"
"time" "time"
"git.smsvc.net/pomodoro/ChronoTomato/internal/helper"
ChronoTomato "git.smsvc.net/pomodoro/ChronoTomato/pkg/models" ChronoTomato "git.smsvc.net/pomodoro/ChronoTomato/pkg/models"
) )
@ -14,10 +14,10 @@ type Client ChronoTomato.GoTomatoClient // New websocket client
func Connect(url string) Client { func Connect(url string) Client {
conn, _, err := websocket.DefaultDialer.Dial(url, nil) conn, _, err := websocket.DefaultDialer.Dial(url, nil)
if err != 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) time.Sleep(time.Second)
return Client{Conn: conn} return Client{Conn: conn}

View file

@ -2,9 +2,9 @@ package websocket
import ( import (
"encoding/json" "encoding/json"
"github.com/charmbracelet/log"
"github.com/gorilla/websocket" "github.com/gorilla/websocket"
"git.smsvc.net/pomodoro/ChronoTomato/internal/helper"
GoTomato "git.smsvc.net/pomodoro/GoTomato/pkg/models" GoTomato "git.smsvc.net/pomodoro/GoTomato/pkg/models"
) )
@ -25,14 +25,14 @@ func (c Client) ProcessServerMessages(channel chan<- GoTomato.ServerMessage) {
return return
} }
// Log any other errors // Log any other errors
log.Error("Read error!", "reason", err) helper.Logger.Error("Read error!", "reason", err)
close(channel) close(channel)
return return
} }
err = json.Unmarshal(message, &serverMessage) err = json.Unmarshal(message, &serverMessage)
if err != nil { if err != nil {
log.Error("Error unmarshalling!", "reason", err) helper.Logger.Error("Error unmarshalling!", "reason", err)
continue continue
} }

View file

@ -2,9 +2,9 @@ package websocket
import ( import (
"encoding/json" "encoding/json"
"github.com/charmbracelet/log"
"github.com/gorilla/websocket" "github.com/gorilla/websocket"
"git.smsvc.net/pomodoro/ChronoTomato/internal/helper"
GoTomato "git.smsvc.net/pomodoro/GoTomato/pkg/models" GoTomato "git.smsvc.net/pomodoro/GoTomato/pkg/models"
) )
@ -12,13 +12,13 @@ import (
func sendClientCommand(c Client, msg GoTomato.ClientCommand) { func sendClientCommand(c Client, msg GoTomato.ClientCommand) {
messageBytes, err := json.Marshal(msg) messageBytes, err := json.Marshal(msg)
if err != nil { if err != nil {
log.Error("Error marshalling!", "reason", err) helper.Logger.Error("Error marshalling!", "reason", err)
return return
} }
err = c.Conn.WriteMessage(websocket.TextMessage, messageBytes) err = c.Conn.WriteMessage(websocket.TextMessage, messageBytes)
if err != nil { if err != nil {
log.Error("Write error!", "reason", err) helper.Logger.Error("Write error!", "reason", err)
return return
} }
} }