1import inspect
2import logging
3import sys
4
5from . import monkey
6
7import distutils.log
8
9
10def _not_warning(record):
11 return record.levelno < logging.WARNING
12
13
14def configure():
15 """
16 Configure logging to emit warning and above to stderr
17 and everything else to stdout. This behavior is provided
18 for compatibility with distutils.log but may change in
19 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_handler
26 logging.basicConfig(
27 format="{message}", style='{', handlers=handlers, level=logging.DEBUG
28 )
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.log
36
37
38def set_threshold(level):
39 logging.root.setLevel(level * 10)
40 return set_threshold.unpatched(level)