Created
December 5, 2012 15:20
-
-
Save bcho/4216472 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #coding: utf-8 | |
| import logging | |
| from logging import StreamHandler | |
| class ColorizingStreamHandler(StreamHandler): | |
| color_map = { | |
| 'black': 0, | |
| 'red': 1, | |
| 'green': 2, | |
| 'yellow': 3, | |
| 'blue': 4, | |
| 'magenta': 5, | |
| 'cyan': 6, | |
| 'white': 7, | |
| } | |
| #: level to (bg, fg, bold/intense) | |
| level_map = { | |
| logging.DEBUG: (None, None, False), | |
| logging.INFO: (None, 'green', False), | |
| logging.WARNING: (None, 'yellow', True), | |
| logging.ERROR: (None, 'red', True), | |
| logging.CRITICAL: ('red', 'white', True), | |
| } | |
| csi = '\x1b[' | |
| reset = '\x1b[0m' | |
| @property | |
| def is_tty(self): | |
| isatty = getattr(self.stream, 'isatty', None) | |
| return isatty and isatty() | |
| def format(self, record): | |
| message = StreamHandler.format(self, record) | |
| if self.is_tty: | |
| parts = message.split('\n', 1) | |
| parts[0] = self.colorize(parts[0], record) | |
| message = '\n'.join(parts) | |
| return message | |
| def colorize(self, message, record): | |
| if record.levelno in self.level_map: | |
| bg, fg, bold = self.level_map[record.levelno] | |
| params = [] | |
| if bg in self.color_map: | |
| params.append(str(self.color_map[bg] + 40)) | |
| if fg in self.color_map: | |
| params.append(str(self.color_map[fg] + 30)) | |
| if bold: | |
| params.append('1') | |
| if params: | |
| message = ''.join((self.csi, ';'.join(params), 'm', message, | |
| self.reset)) | |
| return message |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment