pgtoolkit/coloredformatter.py
author Radek Brich <radek.brich@devl.cz>
Tue, 19 Mar 2013 16:45:37 +0100
changeset 78 64c62ac8f65d
parent 13 16dc5dec9c36
permissions -rw-r--r--
Rename config module to configparser.

import logging

from pgtoolkit import highlight


class ColoredFormatter(logging.Formatter):
    def __init__(self, fmt, datefmt):
        logging.Formatter.__init__(self, fmt, datefmt)

    def format(self, record):
        color = ()
        if record.levelno == logging.ERROR and record.exc_info is not None:
            color = (11, 1)
        if color:
            origmsg = record.msg
            f,b = color
            record.msg = highlight(1, f, b) + record.msg + highlight(0)
            res = logging.Formatter.format(self, record)
            record.msg = origmsg
        else:
            res = logging.Formatter.format(self, record)
        return res