SentryLogger.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. ## 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 = 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)