blob: b301a8338c20dbbf7dee4b92b7b7ede449991ef0 [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):
Tarek Ziadé31d46072009-09-21 13:55:19 +000020 if level not in (DEBUG, INFO, WARN, ERROR, FATAL):
21 raise ValueError('%s wrong log level' % str(level))
22
Jeremy Hylton6fa82a32002-06-04 20:00:26 +000023 if level >= self.threshold:
Benjamin Petersond23f8222009-04-05 19:13:16 +000024 if args:
25 msg = msg % args
26 if level in (WARN, ERROR, FATAL):
27 stream = sys.stderr
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000028 else:
Benjamin Petersond23f8222009-04-05 19:13:16 +000029 stream = sys.stdout
Victor Stinner2ca03c12010-05-19 17:00:07 +000030 if stream.errors == 'strict':
31 # emulate backslashreplace error handler
32 encoding = stream.encoding
33 msg = msg.encode(encoding, "backslashreplace").decode(encoding)
Benjamin Petersond23f8222009-04-05 19:13:16 +000034 stream.write('%s\n' % msg)
35 stream.flush()
Jeremy Hylton6fa82a32002-06-04 20:00:26 +000036
37 def log(self, level, msg, *args):
38 self._log(level, msg, args)
39
40 def debug(self, msg, *args):
41 self._log(DEBUG, msg, args)
Tim Peters182b5ac2004-07-18 06:16:08 +000042
Jeremy Hylton6fa82a32002-06-04 20:00:26 +000043 def info(self, msg, *args):
44 self._log(INFO, msg, args)
Tim Peters182b5ac2004-07-18 06:16:08 +000045
Jeremy Hylton6fa82a32002-06-04 20:00:26 +000046 def warn(self, msg, *args):
47 self._log(WARN, msg, args)
Tim Peters182b5ac2004-07-18 06:16:08 +000048
Jeremy Hylton6fa82a32002-06-04 20:00:26 +000049 def error(self, msg, *args):
50 self._log(ERROR, msg, args)
Tim Peters182b5ac2004-07-18 06:16:08 +000051
Jeremy Hylton6fa82a32002-06-04 20:00:26 +000052 def fatal(self, msg, *args):
53 self._log(FATAL, msg, args)
54
55_global_log = Log()
56log = _global_log.log
57debug = _global_log.debug
58info = _global_log.info
59warn = _global_log.warn
60error = _global_log.error
61fatal = _global_log.fatal
62
63def set_threshold(level):
Fred Drakeedcac8f2004-08-03 18:53:07 +000064 # return the old threshold for use from tests
65 old = _global_log.threshold
Jeremy Hylton6fa82a32002-06-04 20:00:26 +000066 _global_log.threshold = level
Fred Drakeedcac8f2004-08-03 18:53:07 +000067 return old
Jeremy Hylton6fa82a32002-06-04 20:00:26 +000068
69def set_verbosity(v):
Guido van Rossuma85dbeb2003-02-20 02:09:30 +000070 if v <= 0:
Jeremy Hylton6fa82a32002-06-04 20:00:26 +000071 set_threshold(WARN)
Guido van Rossuma85dbeb2003-02-20 02:09:30 +000072 elif v == 1:
Jeremy Hylton6fa82a32002-06-04 20:00:26 +000073 set_threshold(INFO)
Guido van Rossuma85dbeb2003-02-20 02:09:30 +000074 elif v >= 2:
Jeremy Hylton6fa82a32002-06-04 20:00:26 +000075 set_threshold(DEBUG)