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