Created
January 3, 2012 15:14
-
-
Save mdeous/1555294 to your computer and use it in GitHub Desktop.
colored logging formatter
This file contains 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
class ColorFormatter(logging.Formatter): | |
_colors_map = { | |
'DEBUG': '\033[22;32m', | |
'INFO': '\033[01;34m', | |
'WARNING': '\033[22;35m', | |
'ERROR': '\033[22;31m', | |
'CRITICAL': '\033[01;31m' | |
} | |
def format(self, record): | |
level_length = len(record.levelname) | |
if record.levelname in self._colors_map: | |
record.levelname = '{0}{1}\033[0;0m'.format( | |
self._colors_map[record.levelname], | |
record.levelname | |
) | |
# record.levelname += ' ' * (8 - level_length) # aligns logging text (only if levelname is the first element in the format string) | |
record.name = '\033[37m\033[1m{0}\033[0;0m'.format(record.name) | |
return super(ColorFormatter, self).format(record) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment