logger.py 865 B

1234567891011121314151617181920212223242526272829
  1. # -*- coding: utf-8 -*-
  2. import logging
  3. from loguru import logger
  4. class InterceptHandler(logging.Handler):
  5. """
  6. Default handler from examples in loguru documentation.
  7. See https://loguru.readthedocs.io/en/stable/overview.html#entirely-compatible-with-standard-logging
  8. """
  9. def emit(self, record: logging.LogRecord):
  10. # Get corresponding Loguru level if it exists
  11. try:
  12. level = logger.level(record.levelname).name
  13. except ValueError:
  14. level = record.levelno
  15. # Find caller from where originated the logged message
  16. frame, depth = logging.currentframe(), 2
  17. while frame.f_code.co_filename == logging.__file__:
  18. frame = frame.f_back
  19. depth += 1
  20. logger.opt(depth=depth, exception=record.exc_info).log(
  21. level, record.getMessage()
  22. )