Skip to content

Commit

Permalink
Make the logging less verbose for errors (#1118)
Browse files Browse the repository at this point in the history
Only print stack traces if debug level enabled
  • Loading branch information
marcelveldt authored Feb 27, 2024
1 parent 3869d35 commit 79dda66
Show file tree
Hide file tree
Showing 15 changed files with 42 additions and 17 deletions.
7 changes: 6 additions & 1 deletion music_assistant/server/controllers/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,12 @@ async def get(self, cache_key: str, checksum: str | None = None, default=None):
try:
data = await asyncio.to_thread(json_loads, db_row["data"])
except Exception as exc: # pylint: disable=broad-except
LOGGER.exception("Error parsing cache data for %s", cache_key, exc_info=exc)
LOGGER.error(
"Error parsing cache data for %s: %s",
cache_key,
str(exc),
exc_info=exc if self.logger.isEnabledFor(10) else None,
)
else:
# also store in memory cache for faster access
self._mem_cache[cache_key] = (
Expand Down
6 changes: 5 additions & 1 deletion music_assistant/server/controllers/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,11 @@ async def get_playlist_metadata(self, playlist: Playlist) -> None:
MediaItemImage(type=ImageType.THUMB, path=img_path, provider="file")
]
except Exception as err:
LOGGER.debug("Error while creating playlist image", exc_info=err)
LOGGER.warning(
"Error while creating playlist image: %s",
str(err),
exc_info=err if self.logger.isEnabledFor(10) else None,
)
# set timestamp, used to determine when this function was last called
playlist.metadata.last_refresh = int(time())

Expand Down
2 changes: 1 addition & 1 deletion music_assistant/server/controllers/music.py
Original file line number Diff line number Diff line change
Expand Up @@ -601,7 +601,7 @@ def on_sync_task_done(task: asyncio.Task) -> None:
self.logger.warning(
"Sync task for %s completed with errors",
provider.name,
exc_info=task_err,
exc_info=task_err if self.logger.isEnabledFor(10) else None,
)
else:
self.logger.info("Sync task for %s completed", provider.name)
Expand Down
2 changes: 1 addition & 1 deletion music_assistant/server/controllers/players.py
Original file line number Diff line number Diff line change
Expand Up @@ -875,7 +875,7 @@ async def _poll_players(self) -> None:
"Error while requesting latest state from player %s: %s",
player.display_name,
str(err),
exc_info=err,
exc_info=err if self.logger.isEnabledFor(10) else None,
)
finally:
# always update player state
Expand Down
8 changes: 6 additions & 2 deletions music_assistant/server/controllers/webserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@ def __init__(self, webserver: WebserverController, request: web.Request) -> None
self._to_write: asyncio.Queue = asyncio.Queue(maxsize=MAX_PENDING_MSG)
self._handle_task: asyncio.Task | None = None
self._writer_task: asyncio.Task | None = None
self.log_level = webserver.log_level
self._logger = WebSocketLogAdapter(webserver.logger, {"connid": id(self)})

async def disconnect(self) -> None:
Expand Down Expand Up @@ -374,7 +375,10 @@ async def _run_handler(self, handler: APICommandHandler, msg: CommandMessage) ->
result = await result
self._send_message(SuccessResultMessage(msg.message_id, result))
except Exception as err: # pylint: disable=broad-except
self._logger.exception("Error handling message: %s", msg)
if self.log_level == "VERBOSE":
self._logger.exception("Error handling message: %s", msg)
else:
self._logger.error("Error handling message: %s: %s", msg.command, str(err))
self._send_message(
ErrorResultMessage(msg.message_id, getattr(err, "error_code", 999), str(err))
)
Expand Down Expand Up @@ -407,7 +411,7 @@ def _send_message(self, message: MessageType) -> None:
try:
self._to_write.put_nowait(_message)
except asyncio.QueueFull:
self._logger.exception("Client exceeded max pending messages: %s", MAX_PENDING_MSG)
self._logger.error("Client exceeded max pending messages: %s", MAX_PENDING_MSG)

self._cancel()

Expand Down
5 changes: 4 additions & 1 deletion music_assistant/server/providers/dlna/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,10 @@ async def wrapper(self: _DLNAPlayerProviderT, *args: _P.args, **kwargs: _P.kwarg
return await func(self, *args, **kwargs)
except UpnpError as err:
dlna_player.force_poll = True
self.logger.exception("Error during call %s: %r", func.__name__, err)
if self.log_level == "VERBOSE":
self.logger.exception("Error during call %s: %r", func.__name__, err)
else:
self.logger.error("Error during call %s: %r", func.__name__, str(err))
return None

return wrapper
Expand Down
2 changes: 1 addition & 1 deletion music_assistant/server/providers/fanarttv/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ async def _get_data(self, endpoint, **kwargs) -> dict | None:
aiohttp.client_exceptions.ContentTypeError,
JSONDecodeError,
):
self.logger.exception("Failed to retrieve %s", endpoint)
self.logger.error("Failed to retrieve %s", endpoint)
text_result = await response.text()
self.logger.debug(text_result)
return None
Expand Down
9 changes: 7 additions & 2 deletions music_assistant/server/providers/filesystem_local/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ async def sync_library(self, media_types: tuple[MediaType, ...]) -> None:
)
except Exception as err: # pylint: disable=broad-except
# we don't want the whole sync to crash on one file so we catch all exceptions here
self.logger.exception("Error processing %s - %s", item.path, str(err))
self.logger.error("Error processing %s - %s", item.path, str(err))

async def _process_deletions(self, deleted_files: set[str]) -> None:
"""Process all deletions."""
Expand Down Expand Up @@ -504,7 +504,12 @@ async def get_playlist_tracks(
yield media_item

except Exception as err: # pylint: disable=broad-except
self.logger.warning("Error while parsing playlist %s", prov_playlist_id, exc_info=err)
self.logger.warning(
"Error while parsing playlist %s: %s",
prov_playlist_id,
str(err),
exc_info=err if self.logger.isEnabledFor(10) else None,
)

async def _parse_playlist_line(
self, line: str, playlist_path: str, position: int
Expand Down
4 changes: 2 additions & 2 deletions music_assistant/server/providers/qobuz/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -760,9 +760,9 @@ async def _post_data(self, endpoint, params=None, data=None):
JSONDecodeError,
AssertionError,
ValueError,
) as err:
):
text = await response.text()
self.logger.exception("Error while processing %s: %s", endpoint, text, exc_info=err)
self.logger.error("Error while processing %s: %s", endpoint, text)
return None
return result

Expand Down
5 changes: 4 additions & 1 deletion music_assistant/server/providers/sonos/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,10 @@ def do_discover() -> None:
self.logger.debug("Failed to add SonosPlayer %s: %s", soco, err)
except Exception as err:
self.logger.warning(
"Failed to add SonosPlayer %s: %s", soco, err, exc_info=err
"Failed to add SonosPlayer %s: %s",
soco,
err,
exc_info=err if self.logger.isEnabledFor(10) else None,
)
finally:
self._discovery_running = False
Expand Down
2 changes: 1 addition & 1 deletion music_assistant/server/providers/sonos/player.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ def log_subscription_result(self, result: Any, event: str, level: int = logging.
event,
self.zone_name,
message,
exc_info=exc_info,
exc_info=exc_info if self.logger.isEnabledFor(10) else None,
)

async def subscribe(self) -> None:
Expand Down
2 changes: 1 addition & 1 deletion music_assistant/server/providers/spotify/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -752,7 +752,7 @@ async def _get_data(self, endpoint, tokeninfo: dict | None = None, **kwargs):
aiohttp.ContentTypeError,
JSONDecodeError,
):
self.logger.exception("Error while processing %s: %s", endpoint, result)
self.logger.error("Error while processing %s: %s", endpoint, result)
return None
self.logger.debug(
"Processing GET/%s took %s seconds",
Expand Down
2 changes: 1 addition & 1 deletion music_assistant/server/providers/theaudiodb/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ async def _get_data(self, endpoint, **kwargs) -> dict | None:
aiohttp.client_exceptions.ContentTypeError,
JSONDecodeError,
):
self.logger.exception("Failed to retrieve %s", endpoint)
self.logger.error("Failed to retrieve %s", endpoint)
text_result = await response.text()
self.logger.debug(text_result)
return None
Expand Down
2 changes: 1 addition & 1 deletion music_assistant/server/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -603,7 +603,7 @@ async def _setup_discovery(self) -> None:
await self.aiozc.async_register_service(info)
self.mass_zc_service_set = True
except NonUniqueNameException:
LOGGER.exception(
LOGGER.error(
"Music Assistant instance with identical name present in the local network!"
)

Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ ignore = [
"TD003", # Just annoying, not really useful
"TD004", # Just annoying, not really useful
"TRY003", # Just annoying, not really useful
"TRY400", # Just annoying, not really useful
"COM812", # Conflicts with the Ruff formatter
"ISC001", # TEMPORARY DISABLED rules # The below rules must be enabled later one-by-one !
"BLE001",
Expand Down

0 comments on commit 79dda66

Please sign in to comment.