Replace all log.Println and fmt.Printf with Zap structured logging
This commit is contained in:
@@ -1,13 +1,15 @@
|
||||
package backend
|
||||
|
||||
import (
|
||||
"log"
|
||||
"math/rand"
|
||||
"music-server/internal/db"
|
||||
"music-server/internal/db/repository"
|
||||
"music-server/internal/logging"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
type SongInfo struct {
|
||||
@@ -46,7 +48,7 @@ func getAllGames() []repository.Game {
|
||||
func GetSoundCheckSong() string {
|
||||
files, err := os.ReadDir("songs")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
logging.GetLogger().Fatal("Failed to read songs directory", zap.String("error", err.Error()))
|
||||
}
|
||||
fileInfo := files[rand.Intn(len(files))]
|
||||
return "songs/" + fileInfo.Name()
|
||||
@@ -140,7 +142,10 @@ func GetRandomSongClassic() string {
|
||||
|
||||
if err != nil {
|
||||
repo.RemoveBrokenSong(db.Ctx, song.Path)
|
||||
log.Printf("Song not found, song '%s' deleted from game '%s' FileName: %s\n", song.SongName, gameData.GameName, *song.FileName)
|
||||
logging.GetLogger().Warn("Song not found, removed from database",
|
||||
zap.String("song", song.SongName),
|
||||
zap.String("game", gameData.GameName),
|
||||
zap.String("filename", *song.FileName))
|
||||
continue
|
||||
}
|
||||
|
||||
@@ -149,13 +154,16 @@ func GetRandomSongClassic() string {
|
||||
if err != nil || (song.FileName != nil && gameData.Path+*song.FileName != song.Path) {
|
||||
//File not found
|
||||
repo.RemoveBrokenSong(db.Ctx, song.Path)
|
||||
log.Printf("Song not found, song '%s' deleted from game '%s' FileName: %s\n", song.SongName, gameData.GameName, *song.FileName)
|
||||
logging.GetLogger().Warn("Song not found, removed from database",
|
||||
zap.String("song", song.SongName),
|
||||
zap.String("game", gameData.GameName),
|
||||
zap.String("filename", *song.FileName))
|
||||
} else {
|
||||
songFound = true
|
||||
}
|
||||
err = openFile.Close()
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
logging.GetLogger().Error("Failed to close file", zap.String("error", err.Error()))
|
||||
}
|
||||
}
|
||||
lastFetchedNew = song
|
||||
@@ -267,21 +275,24 @@ func getSongFromList(games []repository.Game) repository.Song {
|
||||
continue
|
||||
}
|
||||
song = songs[rand.Intn(len(songs))]
|
||||
log.Println("song = ", song)
|
||||
logging.GetLogger().Debug("Selected song", zap.String("song", song.SongName), zap.String("path", song.Path))
|
||||
|
||||
//Check if file exists and open
|
||||
openFile, err := os.Open(song.Path)
|
||||
if err != nil || (song.FileName != nil && game.Path+*song.FileName != song.Path) || (song.FileName != nil && strings.HasSuffix(*song.FileName, ".wav")) {
|
||||
//File not found
|
||||
repo.RemoveBrokenSong(db.Ctx, song.Path)
|
||||
log.Printf("Song not found, song '%s' deleted from game '%s' FileName: %v\n", song.SongName, game.GameName, song.FileName)
|
||||
logging.GetLogger().Warn("Song not found, removed from database",
|
||||
zap.String("song", song.SongName),
|
||||
zap.String("game", game.GameName),
|
||||
zap.Any("filename", song.FileName))
|
||||
} else {
|
||||
songFound = true
|
||||
}
|
||||
|
||||
err = openFile.Close()
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
logging.GetLogger().Error("Failed to close file", zap.String("path", song.Path), zap.String("error", err.Error()))
|
||||
}
|
||||
}
|
||||
return song
|
||||
|
||||
Reference in New Issue
Block a user