logging.py 1.2 KB

123456789101112131415161718192021222324252627282930313233343536
  1. import sys
  2. import logging
  3. import distutils.log
  4. from . import monkey
  5. def _not_warning(record):
  6. return record.levelno < logging.WARNING
  7. def configure():
  8. """
  9. Configure logging to emit warning and above to stderr
  10. and everything else to stdout. This behavior is provided
  11. for compatibility with distutils.log but may change in
  12. the future.
  13. """
  14. err_handler = logging.StreamHandler()
  15. err_handler.setLevel(logging.WARNING)
  16. out_handler = logging.StreamHandler(sys.stdout)
  17. out_handler.addFilter(_not_warning)
  18. handlers = err_handler, out_handler
  19. logging.basicConfig(
  20. format="{message}", style='{', handlers=handlers, level=logging.DEBUG)
  21. if hasattr(distutils.log, 'Log'):
  22. monkey.patch_func(set_threshold, distutils.log, 'set_threshold')
  23. # For some reason `distutils.log` module is getting cached in `distutils.dist`
  24. # and then loaded again when patched,
  25. # implying: id(distutils.log) != id(distutils.dist.log).
  26. # Make sure the same module object is used everywhere:
  27. distutils.dist.log = distutils.log
  28. def set_threshold(level):
  29. logging.root.setLevel(level*10)
  30. return set_threshold.unpatched(level)