| 7 | import os |
| 8 | |
| 9 | class ColorizingStreamHandler(logging.StreamHandler): |
| 10 | # color names to indices |
| 11 | color_map = { |
| 12 | 'black': 0, |
| 13 | 'red': 1, |
| 14 | 'green': 2, |
| 15 | 'yellow': 3, |
| 16 | 'blue': 4, |
| 17 | 'magenta': 5, |
| 18 | 'cyan': 6, |
| 19 | 'white': 7, |
| 20 | } |
| 21 | |
| 22 | #levels to (background, foreground, bold/intense) |
| 23 | if os.name == 'nt': |
| 24 | level_map = { |
| 25 | logging.DEBUG: (None, 'cyan', True), |
| 26 | logging.INFO: (None, 'blue', False), |
| 27 | logging.WARNING: (None, 'magena', True), |
| 28 | logging.ERROR: (None, 'red', True), |
| 29 | logging.CRITICAL: ('red', 'white', True), |
| 30 | } |
| 31 | else: |
| 32 | level_map = { |
| 33 | logging.DEBUG: (None, 'cyan', True), |
| 34 | logging.INFO: (None, 'blue', False), |
| 35 | logging.WARNING: (None, 'magenta', False), |
| 36 | logging.ERROR: (None, 'red', False), |
| 37 | logging.CRITICAL: ('red', 'white', True), |
| 38 | } |
| 39 | csi = '\x1b[' |
| 40 | reset = '\x1b[0m' |
| 41 | |
| 42 | @property |
| 43 | def is_tty(self): |
| 44 | isatty = getattr(self.stream, 'isatty', None) |
| 45 | return isatty and isatty() |
| 46 | |
| 47 | def emit(self, record): |
| 48 | try: |
| 49 | message = self.format(record) |
| 50 | stream = self.stream |
| 51 | if not self.is_tty: |
| 52 | stream.write(message) |
| 53 | else: |
| 54 | self.output_colorized(message) |
| 55 | stream.write(getattr(self, 'terminator', '\n')) |
| 56 | self.flush() |
| 57 | except (KeyboardInterrupt, SystemExit): |
| 58 | raise |
| 59 | except: |
| 60 | self.handleError(record) |
| 61 | |
| 62 | if os.name != 'nt': |
| 63 | def output_colorized(self, message): |
| 64 | self.stream.write(message) |
| 65 | else: |
| 66 | import re |