pgtoolkit/coloredformatter.py
author Radek Brich <radek.brich@devl.cz>
Sat, 29 Sep 2012 12:08:47 +0200
changeset 48 b82c7c2fb5af
parent 13 16dc5dec9c36
child 78 64c62ac8f65d
permissions -rw-r--r--
PgManager: Fix logging, log queries before executing, possible exceptions are logged after. Add tests for RowDict. Add tests.py - runs all tests.

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 == 21:
            color = (2, None)
        if record.levelno == logging.ERROR and not record.exc_info is 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