Use richer assertions in test_mailbox (for better failure messages).
[python.git] / Lib / distutils / log.py
blob758857081c80e13ec772aea668323961478a752b
1 """A simple log mechanism styled after PEP 282."""
3 # The class here is styled after PEP 282 so that it could later be
4 # replaced with a standard Python logging implementation.
6 DEBUG = 1
7 INFO = 2
8 WARN = 3
9 ERROR = 4
10 FATAL = 5
12 import sys
14 class Log:
16 def __init__(self, threshold=WARN):
17 self.threshold = threshold
19 def _log(self, level, msg, args):
20 if level not in (DEBUG, INFO, WARN, ERROR, FATAL):
21 raise ValueError('%s wrong log level' % str(level))
23 if level >= self.threshold:
24 if args:
25 msg = msg % args
26 if level in (WARN, ERROR, FATAL):
27 stream = sys.stderr
28 else:
29 stream = sys.stdout
30 stream.write('%s\n' % msg)
31 stream.flush()
33 def log(self, level, msg, *args):
34 self._log(level, msg, args)
36 def debug(self, msg, *args):
37 self._log(DEBUG, msg, args)
39 def info(self, msg, *args):
40 self._log(INFO, msg, args)
42 def warn(self, msg, *args):
43 self._log(WARN, msg, args)
45 def error(self, msg, *args):
46 self._log(ERROR, msg, args)
48 def fatal(self, msg, *args):
49 self._log(FATAL, msg, args)
51 _global_log = Log()
52 log = _global_log.log
53 debug = _global_log.debug
54 info = _global_log.info
55 warn = _global_log.warn
56 error = _global_log.error
57 fatal = _global_log.fatal
59 def set_threshold(level):
60 # return the old threshold for use from tests
61 old = _global_log.threshold
62 _global_log.threshold = level
63 return old
65 def set_verbosity(v):
66 if v <= 0:
67 set_threshold(WARN)
68 elif v == 1:
69 set_threshold(INFO)
70 elif v >= 2:
71 set_threshold(DEBUG)