您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 

41 行
1.2 KiB

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