Created
October 5, 2012 07:53
-
-
Save honzajavorek/3838640 to your computer and use it in GitHub Desktop.
Logging formatting
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 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