SentryLogger.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. # Copyright (c) 2019 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. from UM.Logger import LogOutput
  4. from typing import Set
  5. try:
  6. from sentry_sdk import add_breadcrumb
  7. except ImportError:
  8. pass
  9. from typing import Optional
  10. import os
  11. home_dir = os.path.expanduser("~")
  12. class SentryLogger(LogOutput):
  13. # Sentry (https://sentry.io) is the service that Cura uses for logging crashes. This logger ensures that the
  14. # regular log entries that we create are added as breadcrumbs so when a crash actually happens, they are already
  15. # processed and ready for sending.
  16. # Note that this only prepares them for sending. It only sends them when the user actually agrees to sending the
  17. # information.
  18. _levels = {
  19. "w": "warning",
  20. "i": "info",
  21. "c": "fatal",
  22. "e": "error",
  23. "d": "debug"
  24. }
  25. def __init__(self) -> None:
  26. super().__init__()
  27. self._show_once = set() # type: Set[str]
  28. ## Log the message to the sentry hub as a breadcrumb
  29. # \param log_type "e" (error), "i"(info), "d"(debug), "w"(warning) or "c"(critical) (can postfix with "_once")
  30. # \param message String containing message to be logged
  31. def log(self, log_type: str, message: str) -> None:
  32. level = self._translateLogType(log_type)
  33. message = self._pruneSensitiveData(message)
  34. if level is None:
  35. if message not in self._show_once:
  36. level = self._translateLogType(log_type[0])
  37. if level is not None:
  38. self._show_once.add(message)
  39. add_breadcrumb(level = level, message = message)
  40. else:
  41. add_breadcrumb(level = level, message = message)
  42. @staticmethod
  43. def _pruneSensitiveData(message):
  44. if home_dir in message:
  45. message = message.replace(home_dir, "<user_home>")
  46. return message
  47. @staticmethod
  48. def _translateLogType(log_type: str) -> Optional[str]:
  49. return SentryLogger._levels.get(log_type)