_error_interceptor.py 1.1 KB

12345678910111213141516171819202122232425262728293031323334
  1. import sys
  2. import traceback
  3. class ErrorInterceptor:
  4. def __init__(self, should_catch, handler_id):
  5. self._should_catch = should_catch
  6. self._handler_id = handler_id
  7. def should_catch(self):
  8. return self._should_catch
  9. def print(self, record=None, *, exception=None):
  10. if not sys.stderr:
  11. return
  12. if exception is None:
  13. type_, value, traceback_ = sys.exc_info()
  14. else:
  15. type_, value, traceback_ = (type(exception), exception, exception.__traceback__)
  16. try:
  17. sys.stderr.write("--- Logging error in Loguru Handler #%d ---\n" % self._handler_id)
  18. try:
  19. record_repr = str(record)
  20. except Exception:
  21. record_repr = "/!\\ Unprintable record /!\\"
  22. sys.stderr.write("Record was: %s\n" % record_repr)
  23. traceback.print_exception(type_, value, traceback_, None, sys.stderr)
  24. sys.stderr.write("--- End of logging error ---\n")
  25. except OSError:
  26. pass
  27. finally:
  28. del type_, value, traceback_