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
+5 -5
View File
@@ -2,8 +2,8 @@ package server
import (
"github.com/labstack/echo/v5"
"log"
"music-server/internal/backend"
"music-server/internal/logging"
"net/http"
)
@@ -23,7 +23,7 @@ func NewDownloadHandler() *DownloadHandler {
// @Success 200 {string} string
// @Router /download [get]
func (d *DownloadHandler) checkLatest(ctx *echo.Context) error {
log.Println("Checking latest version")
logging.GetLogger().Info("Checking latest version")
latest := backend.CheckLatest()
return ctx.JSON(http.StatusOK, latest)
}
@@ -37,7 +37,7 @@ func (d *DownloadHandler) checkLatest(ctx *echo.Context) error {
// @Success 200 {array} string
// @Router /download/list [get]
func (d *DownloadHandler) listAssetsOfLatest(ctx *echo.Context) error {
log.Println("Listing assets")
logging.GetLogger().Info("Listing assets")
assets := backend.ListAssetsOfLatest()
return ctx.JSON(http.StatusOK, assets)
}
@@ -50,7 +50,7 @@ func (d *DownloadHandler) listAssetsOfLatest(ctx *echo.Context) error {
// @Success 302 {string} string
// @Router /download/windows [get]
func (d *DownloadHandler) downloadLatestWindows(ctx *echo.Context) error {
log.Println("Downloading latest windows")
logging.GetLogger().Info("Downloading latest windows")
asset := backend.DownloadLatestWindows()
ctx.Response().Header().Set("Content-Type", "application/octet-stream")
return ctx.Redirect(http.StatusFound, asset)
@@ -64,7 +64,7 @@ func (d *DownloadHandler) downloadLatestWindows(ctx *echo.Context) error {
// @Success 302 {string} string
// @Router /download/linux [get]
func (d *DownloadHandler) downloadLatestLinux(ctx *echo.Context) error {
log.Println("Downloading latest linux")
logging.GetLogger().Info("Downloading latest linux")
asset := backend.DownloadLatestLinux()
ctx.Response().Header().Set("Content-Type", "application/octet-stream")
return ctx.Redirect(http.StatusFound, asset)
+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()
+2 -1
View File
@@ -11,6 +11,7 @@ import (
"github.com/labstack/echo/v5"
"github.com/labstack/echo/v5/middleware"
echoSwagger "github.com/swaggo/echo-swagger/v2"
"music-server/internal/logging"
)
// @Title MusicServer API
@@ -34,7 +35,7 @@ func (s *Server) RegisterRoutes() http.Handler {
w.Header().Set("Content-Type", "application/json")
http.ServeFile(w, r, "cmd/docs/swagger.json")
})))
e.Use(middleware.RequestLogger())
e.Use(logging.RequestLogger())
e.Use(middleware.Recover())
e.Use(middleware.CORSWithConfig(middleware.CORSConfig{
+26 -8
View File
@@ -2,12 +2,15 @@ package server
import (
"fmt"
"log"
"music-server/internal/db"
"net/http"
"os"
"strconv"
"time"
"music-server/internal/db"
"music-server/internal/logging"
"net/http"
"go.uber.org/zap"
)
type Server struct {
@@ -22,24 +25,39 @@ var (
password = os.Getenv("DB_PASSWORD")
musicPath = os.Getenv("MUSIC_PATH")
charactersPath = os.Getenv("CHARACTERS_PATH")
logLevel = os.Getenv("LOG_LEVEL")
logJSON = os.Getenv("LOG_JSON") == "true"
)
func NewServer() *http.Server {
// Initialize logger
if logLevel == "" {
logLevel = "info"
}
logging.Init(logLevel, logJSON)
logger := logging.GetLogger()
port, _ := strconv.Atoi(os.Getenv("PORT"))
NewServer := &Server{
port: port,
}
fmt.Printf("host: %s, dbPort: %v, username: %s, password: %s, dbName: %s\n",
host, dbPort, username, password, dbName)
logger.Info("Starting server",
zap.String("host", host),
zap.String("dbPort", dbPort),
zap.String("username", username),
zap.String("dbName", dbName),
)
log.Printf("musicPath: %s\n", musicPath)
log.Printf("charactersPath: %s\n", charactersPath)
logger.Info("Paths",
zap.String("musicPath", musicPath),
zap.String("charactersPath", charactersPath),
)
//conf.SetupDb()
if host == "" || dbPort == "" || username == "" || password == "" || dbName == "" || musicPath == "" || charactersPath == "" {
log.Fatal("Invalid settings")
logging.GetLogger().Fatal("Invalid settings - missing required environment variables")
}
db.Migrate_db(host, dbPort, username, password, dbName)
+9 -8
View File
@@ -1,8 +1,8 @@
package server
import (
"log"
"music-server/internal/backend"
"music-server/internal/logging"
"net/http"
"github.com/labstack/echo/v5"
@@ -25,11 +25,11 @@ func NewSyncHandler() *SyncHandler {
// @Router /sync/progress [get]
func (s *SyncHandler) SyncProgress(ctx *echo.Context) error {
if backend.Syncing {
log.Println("Getting progress")
logging.GetLogger().Info("Getting sync progress")
response := backend.SyncProgress()
return ctx.JSON(http.StatusOK, response)
}
log.Println("Getting result")
logging.GetLogger().Info("Getting sync result")
response := backend.SyncResult()
return ctx.JSON(http.StatusOK, response)
}
@@ -45,10 +45,10 @@ func (s *SyncHandler) SyncProgress(ctx *echo.Context) error {
// @Router /sync [get]
func (s *SyncHandler) SyncGamesNewOnlyChanges(ctx *echo.Context) error {
if backend.Syncing {
log.Println("Syncing is in progress")
logging.GetLogger().Warn("Syncing is already in progress")
return ctx.JSON(http.StatusLocked, "Syncing is in progress")
}
log.Println("Start syncing games")
logging.GetLogger().Info("Starting sync with only changes")
go backend.SyncGamesNewOnlyChanges()
return ctx.JSON(http.StatusOK, "Start syncing games")
}
@@ -64,10 +64,10 @@ func (s *SyncHandler) SyncGamesNewOnlyChanges(ctx *echo.Context) error {
// @Router /sync/full [get]
func (s *SyncHandler) SyncGamesNewFull(ctx *echo.Context) error {
if backend.Syncing {
log.Println("Syncing is in progress")
logging.GetLogger().Warn("Syncing is already in progress")
return ctx.JSON(http.StatusLocked, "Syncing is in progress")
}
log.Println("Start syncing games full")
logging.GetLogger().Info("Starting full sync")
go backend.SyncGamesNewFull()
return ctx.JSON(http.StatusOK, "Start syncing games full")
}
@@ -83,9 +83,10 @@ func (s *SyncHandler) SyncGamesNewFull(ctx *echo.Context) error {
// @Router /sync/reset [get]
func (s *SyncHandler) ResetGames(ctx *echo.Context) error {
if backend.Syncing {
log.Println("Syncing is in progress")
logging.GetLogger().Warn("Cannot reset - syncing is in progress")
return ctx.JSON(http.StatusLocked, "Syncing is in progress")
}
logging.GetLogger().Info("Resetting games database")
backend.ResetDB()
return ctx.JSON(http.StatusOK, "Games and songs are deleted from the database")
}