Add missing context to logger calls

This commit is contained in:
Deluan 2022-11-04 11:29:58 -04:00
parent daa428ede7
commit 9c433b5d68
11 changed files with 21 additions and 21 deletions

View file

@ -55,7 +55,7 @@ func (ms *mediaStreamer) NewStream(ctx context.Context, id string, reqFormat str
var bitRate int var bitRate int
var cached bool var cached bool
defer func() { defer func() {
log.Info("Streaming file", "title", mf.Title, "artist", mf.Artist, "format", format, "cached", cached, log.Info(ctx, "Streaming file", "title", mf.Title, "artist", mf.Artist, "format", format, "cached", cached,
"bitRate", bitRate, "user", userName(ctx), "transcoding", format != "raw", "bitRate", bitRate, "user", userName(ctx), "transcoding", format != "raw",
"originalFormat", mf.Suffix, "originalBitRate", mf.BitRate) "originalFormat", mf.Suffix, "originalBitRate", mf.BitRate)
}() }()

View file

@ -46,7 +46,7 @@ func (p *players) Register(ctx context.Context, id, client, userAgent, ip string
Client: client, Client: client,
ScrobbleEnabled: true, ScrobbleEnabled: true,
} }
log.Info("Registering new player", "id", plr.ID, "client", client, "username", userName, "type", userAgent) log.Info(ctx, "Registering new player", "id", plr.ID, "client", client, "username", userName, "type", userAgent)
} }
} }
plr.Name = fmt.Sprintf("%s [%s]", client, userAgent) plr.Name = fmt.Sprintf("%s [%s]", client, userAgent)

View file

@ -131,16 +131,16 @@ func (p *playTracker) Submit(ctx context.Context, submissions []Submission) erro
for _, s := range submissions { for _, s := range submissions {
mf, err := p.ds.MediaFile(ctx).Get(s.TrackID) mf, err := p.ds.MediaFile(ctx).Get(s.TrackID)
if err != nil { if err != nil {
log.Error("Cannot find track for scrobbling", "id", s.TrackID, "user", username, err) log.Error(ctx, "Cannot find track for scrobbling", "id", s.TrackID, "user", username, err)
continue continue
} }
err = p.incPlay(ctx, mf, s.Timestamp) err = p.incPlay(ctx, mf, s.Timestamp)
if err != nil { if err != nil {
log.Error("Error updating play counts", "id", mf.ID, "track", mf.Title, "user", username, err) log.Error(ctx, "Error updating play counts", "id", mf.ID, "track", mf.Title, "user", username, err)
} else { } else {
success++ success++
event.With("song", mf.ID).With("album", mf.AlbumID).With("artist", mf.AlbumArtistID) event.With("song", mf.ID).With("album", mf.AlbumID).With("artist", mf.AlbumArtistID)
log.Info("Scrobbled", "title", mf.Title, "artist", mf.Artist, "user", username, "timestamp", s.Timestamp) log.Info(ctx, "Scrobbled", "title", mf.Title, "artist", mf.Artist, "user", username, "timestamp", s.Timestamp)
if player.ScrobbleEnabled { if player.ScrobbleEnabled {
p.dispatchScrobble(ctx, mf, s.Timestamp) p.dispatchScrobble(ctx, mf, s.Timestamp)
} }

View file

@ -144,7 +144,7 @@ func (r *playlistTrackRepository) getTracks() ([]string, error) {
var ids []string var ids []string
err := r.queryAll(all, &ids) err := r.queryAll(all, &ids)
if err != nil { if err != nil {
log.Error("Error querying current tracks from playlist", "playlistId", r.playlistId, err) log.Error(r.ctx, "Error querying current tracks from playlist", "playlistId", r.playlistId, err)
return nil, err return nil, err
} }
return ids, nil return ids, nil

View file

@ -135,7 +135,7 @@ func createAdmin(ds model.DataStore) func(w http.ResponseWriter, r *http.Request
} }
func createAdminUser(ctx context.Context, ds model.DataStore, username, password string) error { func createAdminUser(ctx context.Context, ds model.DataStore, username, password string) error {
log.Warn("Creating initial user", "user", username) log.Warn(ctx, "Creating initial user", "user", username)
now := time.Now() now := time.Now()
caser := cases.Title(language.Und) caser := cases.Title(language.Und)
initialUser := model.User{ initialUser := model.User{
@ -149,7 +149,7 @@ func createAdminUser(ctx context.Context, ds model.DataStore, username, password
} }
err := ds.User(ctx).Put(&initialUser) err := ds.User(ctx).Put(&initialUser)
if err != nil { if err != nil {
log.Error("Could not create initial user", "user", initialUser, err) log.Error(ctx, "Could not create initial user", "user", initialUser, err)
} }
return nil return nil
} }
@ -199,7 +199,7 @@ func UsernameFromReverseProxyHeader(r *http.Request) string {
return "" return ""
} }
if !validateIPAgainstList(r.RemoteAddr, conf.Server.ReverseProxyWhitelist) { if !validateIPAgainstList(r.RemoteAddr, conf.Server.ReverseProxyWhitelist) {
log.Warn("IP is not whitelisted for reverse proxy login", "ip", r.RemoteAddr) log.Warn(r.Context(), "IP is not whitelisted for reverse proxy login", "ip", r.RemoteAddr)
return "" return ""
} }
username := r.Header.Get(conf.Server.ReverseProxyUserHeader) username := r.Header.Get(conf.Server.ReverseProxyUserHeader)

View file

@ -49,12 +49,12 @@ func handleExportPlaylist(ds model.DataStore) http.HandlerFunc {
plsId := chi.URLParam(r, "playlistId") plsId := chi.URLParam(r, "playlistId")
pls, err := plsRepo.GetWithTracks(plsId) pls, err := plsRepo.GetWithTracks(plsId)
if errors.Is(err, model.ErrNotFound) { if errors.Is(err, model.ErrNotFound) {
log.Warn("Playlist not found", "playlistId", plsId) log.Warn(r.Context(), "Playlist not found", "playlistId", plsId)
http.Error(w, "not found", http.StatusNotFound) http.Error(w, "not found", http.StatusNotFound)
return return
} }
if err != nil { if err != nil {
log.Error("Error retrieving the playlist", "playlistId", plsId, err) log.Error(r.Context(), "Error retrieving the playlist", "playlistId", plsId, err)
http.Error(w, err.Error(), http.StatusInternalServerError) http.Error(w, err.Error(), http.StatusInternalServerError)
return return
} }

View file

@ -27,9 +27,9 @@ var (
translations map[string]translation translations map[string]translation
) )
func newTranslationRepository(context.Context) rest.Repository { func newTranslationRepository(ctx context.Context) rest.Repository {
if err := loadTranslations(resources.FS()); err != nil { if err := loadTranslations(ctx, resources.FS()); err != nil {
log.Error("Error loading translation files", err) log.Error(ctx, "Error loading translation files", err)
} }
return &translationRepository{} return &translationRepository{}
} }
@ -66,7 +66,7 @@ func (r *translationRepository) NewInstance() interface{} {
return &translation{} return &translation{}
} }
func loadTranslations(fsys fs.FS) (loadError error) { func loadTranslations(ctx context.Context, fsys fs.FS) (loadError error) {
once.Do(func() { once.Do(func() {
translations = make(map[string]translation) translations = make(map[string]translation)
dir, err := fsys.Open(consts.I18nFolder) dir, err := fsys.Open(consts.I18nFolder)
@ -83,13 +83,13 @@ func loadTranslations(fsys fs.FS) (loadError error) {
for _, f := range files { for _, f := range files {
t, err := loadTranslation(fsys, f.Name()) t, err := loadTranslation(fsys, f.Name())
if err != nil { if err != nil {
log.Error("Error loading translation file", "file", f.Name(), err) log.Error(ctx, "Error loading translation file", "file", f.Name(), err)
continue continue
} }
translations[t.ID] = t translations[t.ID] = t
languages = append(languages, t.ID) languages = append(languages, t.ID)
} }
log.Info("Loading translations", "languages", languages) log.Info(ctx, "Loading translations", "languages", languages)
}) })
return return
} }

View file

@ -196,7 +196,7 @@ func h(r chi.Router, path string, f handler) {
} }
if r.Context().Err() != nil { if r.Context().Err() != nil {
if log.CurrentLevel() >= log.LevelDebug { if log.CurrentLevel() >= log.LevelDebug {
log.Warn("Request was interrupted", "path", path, r.Context().Err()) log.Warn(r.Context(), "Request was interrupted", "path", path, r.Context().Err())
} }
return return
} }

View file

@ -233,7 +233,7 @@ func (c *MediaAnnotationController) scrobblerNowPlaying(ctx context.Context, tra
clientId = player.ID clientId = player.ID
} }
log.Info("Now Playing", "title", mf.Title, "artist", mf.Artist, "user", username, "player", player.Name) log.Info(ctx, "Now Playing", "title", mf.Title, "artist", mf.Artist, "user", username, "player", player.Name)
err = c.playTracker.NowPlaying(ctx, clientId, client, trackId) err = c.playTracker.NowPlaying(ctx, clientId, client, trackId)
return err return err
} }

View file

@ -149,7 +149,7 @@ func getPlayer(players core.Players) func(next http.Handler) http.Handler {
userAgent := canonicalUserAgent(r) userAgent := canonicalUserAgent(r)
player, trc, err := players.Register(ctx, playerId, client, userAgent, ip) player, trc, err := players.Register(ctx, playerId, client, userAgent, ip)
if err != nil { if err != nil {
log.Error("Could not register player", "username", userName, "client", client, err) log.Error(r.Context(), "Could not register player", "username", userName, "client", client, err)
} else { } else {
ctx = request.WithPlayer(ctx, *player) ctx = request.WithPlayer(ctx, *player)
if trc != nil { if trc != nil {

View file

@ -44,7 +44,7 @@ func (c *StreamController) Stream(w http.ResponseWriter, r *http.Request) (*resp
// Make sure the stream will be closed at the end, to avoid leakage // Make sure the stream will be closed at the end, to avoid leakage
defer func() { defer func() {
if err := stream.Close(); err != nil && log.CurrentLevel() >= log.LevelDebug { if err := stream.Close(); err != nil && log.CurrentLevel() >= log.LevelDebug {
log.Error("Error closing stream", "id", id, "file", stream.Name(), err) log.Error(r.Context(), "Error closing stream", "id", id, "file", stream.Name(), err)
} }
}() }()