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 |
Julien Malard | 0afada1 | 2018-09-09 02:01:26 +0530 | [diff] [blame] | 30 | try: |
| 31 | stream.write('%s\n' % msg) |
| 32 | except UnicodeEncodeError: |
Serhiy Storchaka | 4b860fd | 2018-09-23 09:12:59 +0300 | [diff] [blame] | 33 | # emulate backslashreplace error handler |
| 34 | encoding = stream.encoding |
| 35 | msg = msg.encode(encoding, "backslashreplace").decode(encoding) |
| 36 | stream.write('%s\n' % msg) |
Benjamin Peterson | d23f822 | 2009-04-05 19:13:16 +0000 | [diff] [blame] | 37 | stream.flush() |
Jeremy Hylton | 6fa82a3 | 2002-06-04 20:00:26 +0000 | [diff] [blame] | 38 | |
| 39 | def log(self, level, msg, *args): |
| 40 | self._log(level, msg, args) |
| 41 | |
| 42 | def debug(self, msg, *args): |
| 43 | self._log(DEBUG, msg, args) |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 44 | |
Jeremy Hylton | 6fa82a3 | 2002-06-04 20:00:26 +0000 | [diff] [blame] | 45 | def info(self, msg, *args): |
| 46 | self._log(INFO, msg, args) |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 47 | |
Jeremy Hylton | 6fa82a3 | 2002-06-04 20:00:26 +0000 | [diff] [blame] | 48 | def warn(self, msg, *args): |
| 49 | self._log(WARN, msg, args) |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 50 | |
Jeremy Hylton | 6fa82a3 | 2002-06-04 20:00:26 +0000 | [diff] [blame] | 51 | def error(self, msg, *args): |
| 52 | self._log(ERROR, msg, args) |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 53 | |
Jeremy Hylton | 6fa82a3 | 2002-06-04 20:00:26 +0000 | [diff] [blame] | 54 | def fatal(self, msg, *args): |
| 55 | self._log(FATAL, msg, args) |
| 56 | |
| 57 | _global_log = Log() |
| 58 | log = _global_log.log |
| 59 | debug = _global_log.debug |
| 60 | info = _global_log.info |
| 61 | warn = _global_log.warn |
| 62 | error = _global_log.error |
| 63 | fatal = _global_log.fatal |
| 64 | |
| 65 | def set_threshold(level): |
Fred Drake | edcac8f | 2004-08-03 18:53:07 +0000 | [diff] [blame] | 66 | # return the old threshold for use from tests |
| 67 | old = _global_log.threshold |
Jeremy Hylton | 6fa82a3 | 2002-06-04 20:00:26 +0000 | [diff] [blame] | 68 | _global_log.threshold = level |
Fred Drake | edcac8f | 2004-08-03 18:53:07 +0000 | [diff] [blame] | 69 | return old |
Jeremy Hylton | 6fa82a3 | 2002-06-04 20:00:26 +0000 | [diff] [blame] | 70 | |
| 71 | def set_verbosity(v): |
Guido van Rossum | a85dbeb | 2003-02-20 02:09:30 +0000 | [diff] [blame] | 72 | if v <= 0: |
Jeremy Hylton | 6fa82a3 | 2002-06-04 20:00:26 +0000 | [diff] [blame] | 73 | set_threshold(WARN) |
Guido van Rossum | a85dbeb | 2003-02-20 02:09:30 +0000 | [diff] [blame] | 74 | elif v == 1: |
Jeremy Hylton | 6fa82a3 | 2002-06-04 20:00:26 +0000 | [diff] [blame] | 75 | set_threshold(INFO) |
Guido van Rossum | a85dbeb | 2003-02-20 02:09:30 +0000 | [diff] [blame] | 76 | elif v >= 2: |
Jeremy Hylton | 6fa82a3 | 2002-06-04 20:00:26 +0000 | [diff] [blame] | 77 | set_threshold(DEBUG) |