Skip to content

Instantly share code, notes, and snippets.

@honzajavorek
Created October 5, 2012 07:53
Show Gist options
  • Save honzajavorek/3838640 to your computer and use it in GitHub Desktop.
Save honzajavorek/3838640 to your computer and use it in GitHub Desktop.
Logging formatting
# -*- coding: utf-8 -*-
import logging
from termcolor import colored
class ColoredFormatter(logging.Formatter):
def _colorize(self, record):
record_copy = logging.makeLogRecord(vars(record))
for attr, value in vars(record_copy).items():
palette_name = attr + '_colors'
palette = getattr(self, palette_name, {})
color = palette.get(value, None)
if color:
setattr(record, attr, color(value))
return record
def format(self, record):
return super(ColoredFormatter, self).format(self._colorize(record))
class MyLevelColoredFormatter(ColoredFormatter):
levelname_colors = {
'DEBUG': lambda s: colored(s, 'grey', attrs=['bold']),
'INFO': lambda s: colored(s, 'blue', attrs=['bold']),
'WARNING': lambda s: colored(s, 'yellow', attrs=['bold']),
'ERROR': lambda s: colored(s, 'red', attrs=['bold']),
'CRITICAL': lambda s: colored(s, 'white', 'on_red', attrs=['bold']),
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment