Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Colorizing log handler
# Courtesy http://plumberjack.blogspot.com/2010/12/colorizing-logging-output-in-terminals.html
# Tweaked to use colorama for the coloring
import colorama
import logging
import re
import sys
def remove_ansi(s):
ansi_escape = re.compile(r'(\x9B|\x1B\[)[0-?]*[ -\/]*[@-~]')
return ansi_escape.sub('', s)
class ColorizingStreamHandler(logging.StreamHandler):
color_map = {
logging.DEBUG: colorama.Style.DIM + colorama.Fore.CYAN,
logging.WARNING: colorama.Fore.YELLOW,
logging.ERROR: colorama.Fore.RED,
logging.CRITICAL: colorama.Back.RED,
}
def __init__(self, stream, color_map=None):
logging.StreamHandler.__init__(self,
colorama.AnsiToWin32(stream).stream)
if color_map is not None:
self.color_map = color_map
@property
def is_tty(self):
isatty = getattr(self.stream, 'isatty', None)
return isatty and isatty()
def format(self, record):
message = logging.StreamHandler.format(self, record)
if self.is_tty:
# Don't colorize a traceback
parts = message.split('\n', 1)
parts[0] = self.colorize(parts[0], record)
message = '\n'.join(parts)
else:
message = remove_ansi(message)
return message
def colorize(self, message, record):
try:
return (self.color_map[record.levelno] + message +
colorama.Style.RESET_ALL)
except KeyError:
return message
logger = logging.getLogger('test')
if __name__ == '__main__':
handler = ColorizingStreamHandler(sys.stdout)
formatter = logging.Formatter('%(message)s')
handler.setFormatter(formatter)
logger.addHandler(handler)
logger.setLevel(logging.DEBUG)
logger.debug('debug message')
logger.info('info message')
logger.warning('warning message')
logger.error('error message')
logger.critical('critical message')
logger.error(colorama.Fore.BLUE + 'something' + colorama.Style.RESET_ALL)
@ronanpaixao

This comment has been minimized.

Copy link

ronanpaixao commented Jun 19, 2015

I've added a check to verify if one is running inside an IPython qtconsole. This allows me to use color in the usual QTConsole or Spyder's console.

    def __init__(self, stream, color_map=None):
        logging.StreamHandler.__init__(self,
                                       colorama.AnsiToWin32(stream).stream)
        if color_map is not None:
            self.color_map = color_map

        import __main__
        self.qtconsole = False
        if hasattr(__main__,"get_ipython"):
           import IPython.kernel.zmq.zmqshell as z
           if isinstance(get_ipython(), z.ZMQInteractiveShell):
               self.qtconsole = True

    @property
    def is_tty(self):
        isatty = getattr(self.stream, 'isatty', None)
        return self.qtconsole or isatty and isatty()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.