Skip to content

Instantly share code, notes, and snippets.

@mdeous
Created January 3, 2012 15:14
Show Gist options
  • Save mdeous/1555294 to your computer and use it in GitHub Desktop.
Save mdeous/1555294 to your computer and use it in GitHub Desktop.
colored logging formatter
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