_logging.py 893 B

123456789101112131415161718192021222324252627282930313233
  1. import sys,logging
  2. from loguru import logger
  3. def __exception_handle(e_type, e_value, e_traceback):
  4. if issubclass(e_type, KeyboardInterrupt):
  5. print('\nBye...')
  6. sys.exit(0)
  7. sys.__excepthook__(e_type, e_value, e_traceback)
  8. class __InterceptHandler(logging.Handler):
  9. def emit(self, record):
  10. try:
  11. level = logger.level(record.levelname).name
  12. except ValueError:
  13. level = record.levelno
  14. frame, depth = logging.currentframe(), 2
  15. while frame.f_code.co_filename == logging.__file__:
  16. frame = frame.f_back
  17. depth += 1
  18. logger.opt(depth=depth, exception=record.exc_info).log(
  19. level, record.getMessage()
  20. )
  21. def hook_except_handle():
  22. sys.excepthook = __exception_handle
  23. def hook_logging(**kwargs):
  24. logging.basicConfig(handlers=[__InterceptHandler()], **kwargs)