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 cached bool
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",
"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,
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)

View File

@ -131,16 +131,16 @@ func (p *playTracker) Submit(ctx context.Context, submissions []Submission) erro
for _, s := range submissions {
mf, err := p.ds.MediaFile(ctx).Get(s.TrackID)
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
}
err = p.incPlay(ctx, mf, s.Timestamp)
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 {
success++
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 {
p.dispatchScrobble(ctx, mf, s.Timestamp)
}

View File

@ -144,7 +144,7 @@ func (r *playlistTrackRepository) getTracks() ([]string, error) {
var ids []string
err := r.queryAll(all, &ids)
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 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 {
log.Warn("Creating initial user", "user", username)
log.Warn(ctx, "Creating initial user", "user", username)
now := time.Now()
caser := cases.Title(language.Und)
initialUser := model.User{
@ -149,7 +149,7 @@ func createAdminUser(ctx context.Context, ds model.DataStore, username, password
}
err := ds.User(ctx).Put(&initialUser)
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
}
@ -199,7 +199,7 @@ func UsernameFromReverseProxyHeader(r *http.Request) string {
return ""
}
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 ""
}
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")
pls, err := plsRepo.GetWithTracks(plsId)
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)
return
}
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)
return
}

View File

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

View File

@ -196,7 +196,7 @@ func h(r chi.Router, path string, f handler) {
}
if r.Context().Err() != nil {
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
}

View File

@ -233,7 +233,7 @@ func (c *MediaAnnotationController) scrobblerNowPlaying(ctx context.Context, tra
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)
return err
}

View File

@ -149,7 +149,7 @@ func getPlayer(players core.Players) func(next http.Handler) http.Handler {
userAgent := canonicalUserAgent(r)
player, trc, err := players.Register(ctx, playerId, client, userAgent, ip)
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 {
ctx = request.WithPlayer(ctx, *player)
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
defer func() {
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)
}
}()