Jeremy Hylton | 6fa82a3 | 2002-06-04 20:00:26 +0000 | [diff] [blame] | 1 | """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 | |
| 6 | DEBUG = 1 |
| 7 | INFO = 2 |
| 8 | WARN = 3 |
| 9 | ERROR = 4 |
| 10 | FATAL = 5 |
| 11 | |
Andrew M. Kuchling | e2d1214 | 2002-11-04 14:27:43 +0000 | [diff] [blame] | 12 | import sys |
| 13 | |
Jeremy Hylton | 6fa82a3 | 2002-06-04 20:00:26 +0000 | [diff] [blame] | 14 | class Log: |
| 15 | |
| 16 | def __init__(self, threshold=WARN): |
| 17 | self.threshold = threshold |
| 18 | |
| 19 | def _log(self, level, msg, args): |
Tarek Ziadé | 31d4607 | 2009-09-21 13:55:19 +0000 | [diff] [blame] | 20 | if level not in (DEBUG, INFO, WARN, ERROR, FATAL): |
| 21 | raise ValueError('%s wrong log level' % str(level)) |
| 22 | |
Jeremy Hylton | 6fa82a3 | 2002-06-04 20:00:26 +0000 | [diff] [blame] | 23 | if level >= self.threshold: |
Benjamin Peterson | d23f822 | 2009-04-05 19:13:16 +0000 | [diff] [blame] | 24 | if args: |
| 25 | msg = msg % args |
| 26 | if level in (WARN, ERROR, FATAL): |
| 27 | stream = sys.stderr |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 28 | else: |
Benjamin Peterson | d23f822 | 2009-04-05 19:13:16 +0000 | [diff] [blame] | 29 | stream = sys.stdout |
Victor Stinner | 2ca03c1 | 2010-05-19 17:00:07 +0000 | [diff] [blame^] | 30 | if stream.errors == 'strict': |
| 31 | # emulate backslashreplace error handler |
| 32 | encoding = stream.encoding |
| 33 | msg = msg.encode(encoding, "backslashreplace").decode(encoding) |
Benjamin Peterson | d23f822 | 2009-04-05 19:13:16 +0000 | [diff] [blame] | 34 | stream.write('%s\n' % msg) |
| 35 | stream.flush() |
Jeremy Hylton | 6fa82a3 | 2002-06-04 20:00:26 +0000 | [diff] [blame] | 36 | |
| 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 Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 42 | |
Jeremy Hylton | 6fa82a3 | 2002-06-04 20:00:26 +0000 | [diff] [blame] | 43 | def info(self, msg, *args): |
| 44 | self._log(INFO, msg, args) |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 45 | |
Jeremy Hylton | 6fa82a3 | 2002-06-04 20:00:26 +0000 | [diff] [blame] | 46 | def warn(self, msg, *args): |
| 47 | self._log(WARN, msg, args) |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 48 | |
Jeremy Hylton | 6fa82a3 | 2002-06-04 20:00:26 +0000 | [diff] [blame] | 49 | def error(self, msg, *args): |
| 50 | self._log(ERROR, msg, args) |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 51 | |
Jeremy Hylton | 6fa82a3 | 2002-06-04 20:00:26 +0000 | [diff] [blame] | 52 | def fatal(self, msg, *args): |
| 53 | self._log(FATAL, msg, args) |
| 54 | |
| 55 | _global_log = Log() |
| 56 | log = _global_log.log |
| 57 | debug = _global_log.debug |
| 58 | info = _global_log.info |
| 59 | warn = _global_log.warn |
| 60 | error = _global_log.error |
| 61 | fatal = _global_log.fatal |
| 62 | |
| 63 | def set_threshold(level): |
Fred Drake | edcac8f | 2004-08-03 18:53:07 +0000 | [diff] [blame] | 64 | # return the old threshold for use from tests |
| 65 | old = _global_log.threshold |
Jeremy Hylton | 6fa82a3 | 2002-06-04 20:00:26 +0000 | [diff] [blame] | 66 | _global_log.threshold = level |
Fred Drake | edcac8f | 2004-08-03 18:53:07 +0000 | [diff] [blame] | 67 | return old |
Jeremy Hylton | 6fa82a3 | 2002-06-04 20:00:26 +0000 | [diff] [blame] | 68 | |
| 69 | def set_verbosity(v): |
Guido van Rossum | a85dbeb | 2003-02-20 02:09:30 +0000 | [diff] [blame] | 70 | if v <= 0: |
Jeremy Hylton | 6fa82a3 | 2002-06-04 20:00:26 +0000 | [diff] [blame] | 71 | set_threshold(WARN) |
Guido van Rossum | a85dbeb | 2003-02-20 02:09:30 +0000 | [diff] [blame] | 72 | elif v == 1: |
Jeremy Hylton | 6fa82a3 | 2002-06-04 20:00:26 +0000 | [diff] [blame] | 73 | set_threshold(INFO) |
Guido van Rossum | a85dbeb | 2003-02-20 02:09:30 +0000 | [diff] [blame] | 74 | elif v >= 2: |
Jeremy Hylton | 6fa82a3 | 2002-06-04 20:00:26 +0000 | [diff] [blame] | 75 | set_threshold(DEBUG) |