Add Zap logging framework with structured logging for Echo and Grafana

This commit is contained in:
2026-05-20 22:29:45 +02:00
parent 82252ce1ff
commit 37909139de
7 changed files with 64 additions and 37 deletions
+16 -15
View File
@@ -1,13 +1,14 @@
package server
import (
"log"
"music-server/internal/backend"
"music-server/internal/logging"
"net/http"
"os"
"strconv"
"github.com/labstack/echo/v5"
"go.uber.org/zap"
)
type MusicHandler struct {
@@ -31,7 +32,7 @@ func NewMusicHandler() *MusicHandler {
// @Router /music [get]
func (m *MusicHandler) GetSong(ctx *echo.Context) error {
if backend.Syncing {
log.Println("Syncing is in progress")
logging.GetLogger().Info("Syncing is in progress")
return ctx.JSON(http.StatusLocked, "Syncing is in progress")
}
song := ctx.QueryParam("song")
@@ -58,7 +59,7 @@ func (m *MusicHandler) GetSong(ctx *echo.Context) error {
// @Router /music/soundTest [get]
func (m *MusicHandler) GetSoundCheckSong(ctx *echo.Context) error {
if backend.Syncing {
log.Println("Syncing is in progress")
logging.GetLogger().Info("Syncing is in progress")
return ctx.JSON(http.StatusLocked, "Syncing is in progress")
}
songPath := backend.GetSoundCheckSong()
@@ -80,7 +81,7 @@ func (m *MusicHandler) GetSoundCheckSong(ctx *echo.Context) error {
// @Router /music/reset [get]
func (m *MusicHandler) ResetMusic(ctx *echo.Context) error {
if backend.Syncing {
log.Println("Syncing is in progress")
logging.GetLogger().Info("Syncing is in progress")
return ctx.JSON(http.StatusLocked, "Syncing is in progress")
}
backend.Reset()
@@ -98,7 +99,7 @@ func (m *MusicHandler) ResetMusic(ctx *echo.Context) error {
// @Router /music/rand [get]
func (m *MusicHandler) GetRandomSong(ctx *echo.Context) error {
if backend.Syncing {
log.Println("Syncing is in progress")
logging.GetLogger().Info("Syncing is in progress")
return ctx.JSON(http.StatusLocked, "Syncing is in progress")
}
songPath := backend.GetRandomSong()
@@ -121,7 +122,7 @@ func (m *MusicHandler) GetRandomSong(ctx *echo.Context) error {
// @Router /music/rand/low [get]
func (m *MusicHandler) GetRandomSongLowChance(ctx *echo.Context) error {
if backend.Syncing {
log.Println("Syncing is in progress")
logging.GetLogger().Info("Syncing is in progress")
return ctx.JSON(http.StatusLocked, "Syncing is in progress")
}
songPath := backend.GetRandomSongLowChance()
@@ -144,7 +145,7 @@ func (m *MusicHandler) GetRandomSongLowChance(ctx *echo.Context) error {
// @Router /music/rand/classic [get]
func (m *MusicHandler) GetRandomSongClassic(ctx *echo.Context) error {
if backend.Syncing {
log.Println("Syncing is in progress")
logging.GetLogger().Info("Syncing is in progress")
return ctx.JSON(http.StatusLocked, "Syncing is in progress")
}
songPath := backend.GetRandomSongClassic()
@@ -193,7 +194,7 @@ func (m *MusicHandler) GetPlayedSongs(ctx *echo.Context) error {
// @Router /music/next [get]
func (m *MusicHandler) GetNextSong(ctx *echo.Context) error {
if backend.Syncing {
log.Println("Syncing is in progress")
logging.GetLogger().Info("Syncing is in progress")
return ctx.JSON(http.StatusLocked, "Syncing is in progress")
}
songPath := backend.GetNextSong()
@@ -216,7 +217,7 @@ func (m *MusicHandler) GetNextSong(ctx *echo.Context) error {
// @Router /music/previous [get]
func (m *MusicHandler) GetPreviousSong(ctx *echo.Context) error {
if backend.Syncing {
log.Println("Syncing is in progress")
logging.GetLogger().Info("Syncing is in progress")
return ctx.JSON(http.StatusLocked, "Syncing is in progress")
}
songPath := backend.GetPreviousSong()
@@ -239,7 +240,7 @@ func (m *MusicHandler) GetPreviousSong(ctx *echo.Context) error {
// @Router /music/all/order [get]
func (m *MusicHandler) GetAllGames(ctx *echo.Context) error {
if backend.Syncing {
log.Println("Syncing is in progress")
logging.GetLogger().Info("Syncing is in progress")
return ctx.JSON(http.StatusLocked, "Syncing is in progress")
}
gameList := backend.GetAllGames()
@@ -257,7 +258,7 @@ func (m *MusicHandler) GetAllGames(ctx *echo.Context) error {
// @Router /music/all/random [get]
func (m *MusicHandler) GetAllGamesRandom(ctx *echo.Context) error {
if backend.Syncing {
log.Println("Syncing is in progress")
logging.GetLogger().Info("Syncing is in progress")
return ctx.JSON(http.StatusLocked, "Syncing is in progress")
}
gameList := backend.GetAllGamesRandom()
@@ -277,14 +278,14 @@ func (m *MusicHandler) GetAllGamesRandom(ctx *echo.Context) error {
// @Router /music/played [put]
func (m *MusicHandler) PutPlayed(ctx *echo.Context) error {
if backend.Syncing {
log.Println("Syncing is in progress")
logging.GetLogger().Info("Syncing is in progress")
return ctx.JSON(http.StatusLocked, "Syncing is in progress")
}
song, err := strconv.Atoi(ctx.QueryParam("song"))
if err != nil {
return ctx.JSON(http.StatusBadRequest, err.Error())
}
log.Println("song", song)
logging.GetLogger().Info("Marking song as played", zap.Int("song_id", song))
backend.SetPlayed(song)
return ctx.NoContent(http.StatusOK)
}
@@ -299,7 +300,7 @@ func (m *MusicHandler) PutPlayed(ctx *echo.Context) error {
// @Router /music/addQue [get]
func (m *MusicHandler) AddLatestToQue(ctx *echo.Context) error {
if backend.Syncing {
log.Println("Syncing is in progress")
logging.GetLogger().Info("Syncing is in progress")
return ctx.JSON(http.StatusLocked, "Syncing is in progress")
}
backend.AddLatestToQue()
@@ -316,7 +317,7 @@ func (m *MusicHandler) AddLatestToQue(ctx *echo.Context) error {
// @Router /music/addPlayed [get]
func (m *MusicHandler) AddLatestPlayed(ctx *echo.Context) error {
if backend.Syncing {
log.Println("Syncing is in progress")
logging.GetLogger().Info("Syncing is in progress")
return ctx.JSON(http.StatusLocked, "Syncing is in progress")
}
backend.AddLatestPlayed()