Index: Lib/distutils/log.py =================================================================== --- Lib/distutils/log.py (revision 72360) +++ Lib/distutils/log.py (working copy) @@ -1,68 +1,64 @@ """A simple log mechanism styled after PEP 282.""" +import sys -# The class here is styled after PEP 282 so that it could later be -# replaced with a standard Python logging implementation. +try: + import logging + logger = logging.getLogger('distutils') -DEBUG = 1 -INFO = 2 -WARN = 3 -ERROR = 4 -FATAL = 5 + def set_verbosity(level): + logging.basicConfig(format="%(levelname)s:%(name)s:%(module)s(%(lineno)d): %(message)s") + logging.getLogger().setLevel(logging.WARNING) + if level == 1: + logging.getLogger().setLevel(logging.INFO) + elif level >= 2: + logging.getLogger().setLevel(logging.DEBUG) -import sys +except ImportError: + # At build time the logging module requires support + # modules the might not be available so we switch back + # to a simpler implementation -class Log: + class Logger: + stream = sys.stderr - def __init__(self, threshold=WARN): - self.threshold = threshold + def __init__(self): + self.level = 0 - def _log(self, level, msg, args): - if level >= self.threshold: - if args: - msg = msg % args - if level in (WARN, ERROR, FATAL): - stream = sys.stderr - else: - stream = sys.stdout - stream.write('%s\n' % msg) - stream.flush() + def _log(self, level, msg): + if self.level >= level: + Logger.stream.write(msg + "\n") - def log(self, level, msg, *args): - self._log(level, msg, args) + def debug(self, msg, *args, **kwargs): + self._log(2, "DEBUG: " + msg % args) - def debug(self, msg, *args): - self._log(DEBUG, msg, args) + def info(self, msg, *args, **kwargs): + self._log(1, "INFO: " + msg % args) - def info(self, msg, *args): - self._log(INFO, msg, args) + def warn(self, msg, *args, **kwargs): + self._log(0, "WARN: " + msg % args) - def warn(self, msg, *args): - self._log(WARN, msg, args) + def warning(self, msg, *args, **kwargs): + self._log(0, "WARN: " + msg % args) - def error(self, msg, *args): - self._log(ERROR, msg, args) + def error(self, msg, *args, **kwargs): + self._log(0, "ERROR: " + msg % args) - def fatal(self, msg, *args): - self._log(FATAL, msg, args) + def critical(self, msg, *args, **kwargs): + self._log(0, "FATAL: " + msg % args) -_global_log = Log() -log = _global_log.log -debug = _global_log.debug -info = _global_log.info -warn = _global_log.warn -error = _global_log.error -fatal = _global_log.fatal + logger = Logger() + def set_verbosity(level): + if level == 1: + Logger.level = 1 + elif level >= 2: + Logger.level = 2 -def set_threshold(level): - # return the old threshold for use from tests - old = _global_log.threshold - _global_log.threshold = level - return old -def set_verbosity(v): - if v <= 0: - set_threshold(WARN) - elif v == 1: - set_threshold(INFO) - elif v >= 2: - set_threshold(DEBUG) +# These are to keep the api stable +debug = logger.debug +info = logger.info +warn = logger.warning +warning = logger.warning +error = logger.error +fatal = logger.critical +