blob: 6aeb7c9aac5843591b47d4c2bd69f5015d8533a5 [file] [log] [blame]
Jeremy Hylton6fa82a32002-06-04 20:00:26 +00001"""A simple log mechanism styled after PEP 282."""
2
3# The class here is styled after PEP 282 so that it could later be
4# replaced with a standard Python logging implementation.
5
6DEBUG = 1
7INFO = 2
8WARN = 3
9ERROR = 4
10FATAL = 5
11
Andrew M. Kuchlinge2d12142002-11-04 14:27:43 +000012import sys
13
Jeremy Hylton6fa82a32002-06-04 20:00:26 +000014class Log:
15
16 def __init__(self, threshold=WARN):
17 self.threshold = threshold
18
19 def _log(self, level, msg, args):
20 if level >= self.threshold:
21 print msg % args
Andrew M. Kuchlinge2d12142002-11-04 14:27:43 +000022 sys.stdout.flush()
Jeremy Hylton6fa82a32002-06-04 20:00:26 +000023
24 def log(self, level, msg, *args):
25 self._log(level, msg, args)
26
27 def debug(self, msg, *args):
28 self._log(DEBUG, msg, args)
29
30 def info(self, msg, *args):
31 self._log(INFO, msg, args)
32
33 def warn(self, msg, *args):
34 self._log(WARN, msg, args)
35
36 def error(self, msg, *args):
37 self._log(ERROR, msg, args)
38
39 def fatal(self, msg, *args):
40 self._log(FATAL, msg, args)
41
42_global_log = Log()
43log = _global_log.log
44debug = _global_log.debug
45info = _global_log.info
46warn = _global_log.warn
47error = _global_log.error
48fatal = _global_log.fatal
49
50def set_threshold(level):
51 _global_log.threshold = level
52
53def set_verbosity(v):
54 if v == 0:
55 set_threshold(WARN)
56 if v == 1:
57 set_threshold(INFO)
58 if v == 2:
59 set_threshold(DEBUG)