SentryLogger.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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. from cura.CrashHandler import CrashHandler
  6. try:
  7. from sentry_sdk import add_breadcrumb
  8. except ImportError:
  9. pass
  10. from typing import Optional
  11. import os
  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. def log(self, log_type: str, message: str) -> None:
  29. """Log the message to the sentry hub as a breadcrumb
  30. :param log_type: "e" (error), "i"(info), "d"(debug), "w"(warning) or "c"(critical) (can postfix with "_once")
  31. :param message: String containing message to be logged
  32. """
  33. level = self._translateLogType(log_type)
  34. message = CrashHandler.pruneSensitiveData(message)
  35. if level is None:
  36. if message not in self._show_once:
  37. level = self._translateLogType(log_type[0])
  38. if level is not None:
  39. self._show_once.add(message)
  40. add_breadcrumb(level = level, message = message)
  41. else:
  42. add_breadcrumb(level = level, message = message)
  43. @staticmethod
  44. def _translateLogType(log_type: str) -> Optional[str]:
  45. return SentryLogger._levels.get(log_type)