_log.py 1015 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. """Customize logging
  2. Defines custom logger class for the `logger.verbose(...)` method.
  3. init_logging() must be called before any other modules that call logging.getLogger.
  4. """
  5. import logging
  6. from typing import Any, cast
  7. # custom log level for `--verbose` output
  8. # between DEBUG and INFO
  9. VERBOSE = 15
  10. class VerboseLogger(logging.Logger):
  11. """Custom Logger, defining a verbose log-level
  12. VERBOSE is between INFO and DEBUG.
  13. """
  14. def verbose(self, msg: str, *args: Any, **kwargs: Any) -> None:
  15. return self.log(VERBOSE, msg, *args, **kwargs)
  16. def getLogger(name: str) -> VerboseLogger:
  17. """logging.getLogger, but ensures our VerboseLogger class is returned"""
  18. return cast(VerboseLogger, logging.getLogger(name))
  19. def init_logging() -> None:
  20. """Register our VerboseLogger and VERBOSE log level.
  21. Should be called before any calls to getLogger(),
  22. i.e. in pip._internal.__init__
  23. """
  24. logging.setLoggerClass(VerboseLogger)
  25. logging.addLevelName(VERBOSE, "VERBOSE")