SentryLogger.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. class SentryLogger(LogOutput):
  12. # Sentry (https://sentry.io) is the service that Cura uses for logging crashes. This logger ensures that the
  13. # regular log entries that we create are added as breadcrumbs so when a crash actually happens, they are already
  14. # processed and ready for sending.
  15. # Note that this only prepares them for sending. It only sends them when the user actually agrees to sending the
  16. # information.
  17. _levels = {
  18. "w": "warning",
  19. "i": "info",
  20. "c": "fatal",
  21. "e": "error",
  22. "d": "debug"
  23. }
  24. def __init__(self) -> None:
  25. super().__init__()
  26. self._show_once = set() # type: Set[str]
  27. def log(self, log_type: str, message: str) -> None:
  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. """
  32. level = self._translateLogType(log_type)
  33. message = CrashHandler.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 _translateLogType(log_type: str) -> Optional[str]:
  44. return SentryLogger._levels.get(log_type)