codekingpro/portable-devtools
114k
1import inspect2import logging3import sys4 5from . import monkey6 7import distutils.log8 9 10def _not_warning(record):11 return record.levelno < logging.WARNING12 13 14def configure() -> None:15 """16 Configure logging to emit warning and above to stderr17 and everything else to stdout. This behavior is provided18 for compatibility with distutils.log but may change in19 the future.20 """21 err_handler = logging.StreamHandler()22 err_handler.setLevel(logging.WARNING)23 out_handler = logging.StreamHandler(sys.stdout)24 out_handler.addFilter(_not_warning)25 handlers = err_handler, out_handler26 logging.basicConfig(27 format="{message}", style='{', handlers=handlers, level=logging.DEBUG28 )29 if inspect.ismodule(distutils.dist.log):30 monkey.patch_func(set_threshold, distutils.log, 'set_threshold')31 # For some reason `distutils.log` module is getting cached in `distutils.dist`32 # and then loaded again when patched,33 # implying: id(distutils.log) != id(distutils.dist.log).34 # Make sure the same module object is used everywhere:35 distutils.dist.log = distutils.log36 37 38def set_threshold(level: int) -> int:39 logging.root.setLevel(level * 10)40 return set_threshold.unpatched(level)41 