Replace all log.Println and fmt.Printf with Zap structured logging

This commit is contained in:
2026-05-21 23:24:55 +02:00
parent f0653489d6
commit 89c31c2856
7 changed files with 184 additions and 162 deletions
+18 -21
View File
@@ -2,10 +2,11 @@ package backend
import (
"encoding/json"
"fmt"
"log"
"net/http"
"strings"
"music-server/internal/logging"
"go.uber.org/zap"
)
type giteaResponse struct {
@@ -23,37 +24,35 @@ type assetResponse struct {
func CheckLatest() string {
resp, err := http.Get("https://gitea.sanplex.xyz/api/v1/repos/sansan/MusicPlayer/releases/latest")
if err != nil {
log.Fatalln(err)
logging.GetLogger().Fatal("Failed to check latest version", zap.String("error", err.Error()))
}
defer resp.Body.Close()
//Create a variable of the same type as our model
var cResp giteaResponse
//Decode the data
if err := json.NewDecoder(resp.Body).Decode(&cResp); err != nil {
fmt.Println(err)
log.Fatal("ooopsss! an error occurred, please try again")
logging.GetLogger().Fatal("Failed to decode response", zap.String("error", err.Error()))
}
log.Printf("Id: %v, Name: %v", cResp.Id, cResp.Name)
logging.GetLogger().Debug("Checked latest version", zap.Int("id", cResp.Id), zap.String("name", cResp.Name))
return cResp.Name
}
func ListAssetsOfLatest() []string {
resp, err := http.Get("https://gitea.sanplex.xyz/api/v1/repos/sansan/MusicPlayer/releases/latest")
if err != nil {
log.Fatalln(err)
logging.GetLogger().Fatal("Failed to list assets", zap.String("error", err.Error()))
}
defer resp.Body.Close()
//Create a variable of the same type as our model
var cResp giteaResponse
//Decode the data
if err := json.NewDecoder(resp.Body).Decode(&cResp); err != nil {
fmt.Println(err)
log.Fatal("ooopsss! an error occurred, please try again")
logging.GetLogger().Fatal("Failed to decode response", zap.String("error", err.Error()))
}
log.Printf("Id: %v, Name: %v", cResp.Id, cResp.Name)
logging.GetLogger().Debug("Listing assets", zap.Int("id", cResp.Id), zap.String("name", cResp.Name))
var assets []string
for _, asset := range cResp.Assets {
log.Printf("Id: %v, Name: %v, Asset: %v", cResp.Id, cResp.Name, asset.Name)
logging.GetLogger().Debug("Found asset", zap.Int("id", cResp.Id), zap.String("name", cResp.Name), zap.String("asset", asset.Name))
assets = append(assets, asset.Name)
}
return assets
@@ -62,19 +61,18 @@ func ListAssetsOfLatest() []string {
func DownloadLatestWindows() string {
resp, err := http.Get("https://gitea.sanplex.xyz/api/v1/repos/sansan/MusicPlayer/releases/latest")
if err != nil {
log.Fatalln(err)
logging.GetLogger().Fatal("Failed to download latest Windows version", zap.String("error", err.Error()))
}
defer resp.Body.Close()
//Create a variable of the same type as our model
var cResp giteaResponse
//Decode the data
if err := json.NewDecoder(resp.Body).Decode(&cResp); err != nil {
fmt.Println(err)
log.Fatal("ooopsss! an error occurred, please try again")
logging.GetLogger().Fatal("Failed to decode response", zap.String("error", err.Error()))
}
log.Printf("Id: %v, Name: %v", cResp.Id, cResp.Name)
logging.GetLogger().Debug("Downloading Windows version", zap.Int("id", cResp.Id), zap.String("name", cResp.Name))
for _, asset := range cResp.Assets {
log.Printf("Id: %v, Name: %v, Asset: %v", cResp.Id, cResp.Name, asset.Name)
logging.GetLogger().Debug("Checking asset", zap.Int("id", cResp.Id), zap.String("name", cResp.Name), zap.String("asset", asset.Name))
if strings.HasSuffix(asset.Name, ".exe") {
return asset.DownloadUrl
}
@@ -85,19 +83,18 @@ func DownloadLatestWindows() string {
func DownloadLatestLinux() string {
resp, err := http.Get("https://gitea.sanplex.xyz/api/v1/repos/sansan/MusicPlayer/releases/latest")
if err != nil {
log.Fatalln(err)
logging.GetLogger().Fatal("Failed to download latest Linux version", zap.String("error", err.Error()))
}
defer resp.Body.Close()
//Create a variable of the same type as our model
var cResp giteaResponse
//Decode the data
if err := json.NewDecoder(resp.Body).Decode(&cResp); err != nil {
fmt.Println(err)
log.Fatal("ooopsss! an error occurred, please try again")
logging.GetLogger().Fatal("Failed to decode response", zap.String("error", err.Error()))
}
log.Printf("Id: %v, Name: %v", cResp.Id, cResp.Name)
logging.GetLogger().Debug("Downloading Linux version", zap.Int("id", cResp.Id), zap.String("name", cResp.Name))
for _, asset := range cResp.Assets {
log.Printf("Id: %v, Name: %v, Asset: %v", cResp.Id, cResp.Name, asset.Name)
logging.GetLogger().Debug("Checking asset", zap.Int("id", cResp.Id), zap.String("name", cResp.Name), zap.String("asset", asset.Name))
if strings.HasSuffix(asset.Name, ".x86_64") {
return asset.DownloadUrl
}