log.py 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. """
  2. A simple log mechanism styled after PEP 282.
  3. Retained for compatibility and should not be used.
  4. """
  5. import logging
  6. import warnings
  7. from ._log import log as _global_log
  8. DEBUG = logging.DEBUG
  9. INFO = logging.INFO
  10. WARN = logging.WARN
  11. ERROR = logging.ERROR
  12. FATAL = logging.FATAL
  13. log = _global_log.log
  14. debug = _global_log.debug
  15. info = _global_log.info
  16. warn = _global_log.warning
  17. error = _global_log.error
  18. fatal = _global_log.fatal
  19. def set_threshold(level):
  20. orig = _global_log.level
  21. _global_log.setLevel(level)
  22. return orig
  23. def set_verbosity(v):
  24. if v <= 0:
  25. set_threshold(logging.WARN)
  26. elif v == 1:
  27. set_threshold(logging.INFO)
  28. elif v >= 2:
  29. set_threshold(logging.DEBUG)
  30. class Log(logging.Logger):
  31. """distutils.log.Log is deprecated, please use an alternative from `logging`."""
  32. def __init__(self, threshold=WARN):
  33. warnings.warn(Log.__doc__) # avoid DeprecationWarning to ensure warn is shown
  34. super().__init__(__name__, level=threshold)
  35. @property
  36. def threshold(self):
  37. return self.level
  38. @threshold.setter
  39. def threshold(self, level):
  40. self.setLevel(level)
  41. warn = logging.Logger.warning