blob: f0a7865067c4c4acd3387705485e1e1fec0d03ad [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
12class Log:
13
14 def __init__(self, threshold=WARN):
15 self.threshold = threshold
16
17 def _log(self, level, msg, args):
18 if level >= self.threshold:
19 print msg % args
20
21 def log(self, level, msg, *args):
22 self._log(level, msg, args)
23
24 def debug(self, msg, *args):
25 self._log(DEBUG, msg, args)
26
27 def info(self, msg, *args):
28 self._log(INFO, msg, args)
29
30 def warn(self, msg, *args):
31 self._log(WARN, msg, args)
32
33 def error(self, msg, *args):
34 self._log(ERROR, msg, args)
35
36 def fatal(self, msg, *args):
37 self._log(FATAL, msg, args)
38
39_global_log = Log()
40log = _global_log.log
41debug = _global_log.debug
42info = _global_log.info
43warn = _global_log.warn
44error = _global_log.error
45fatal = _global_log.fatal
46
47def set_threshold(level):
48 _global_log.threshold = level
49
50def set_verbosity(v):
51 if v == 0:
52 set_threshold(WARN)
53 if v == 1:
54 set_threshold(INFO)
55 if v == 2:
56 set_threshold(DEBUG)