pgtoolkit/coloredformatter.py
author Radek Brich <radek.brich@devl.cz>
Fri, 09 Sep 2011 11:56:37 +0200
changeset 13 16dc5dec9c36
child 78 64c62ac8f65d
permissions -rw-r--r--
ToolBase: add -d parameter, which triggers debug mode (print SQL queries).
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
13
16dc5dec9c36 ToolBase: add -d parameter, which triggers debug mode (print SQL queries).
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     1
import logging
16dc5dec9c36 ToolBase: add -d parameter, which triggers debug mode (print SQL queries).
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     2
16dc5dec9c36 ToolBase: add -d parameter, which triggers debug mode (print SQL queries).
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     3
from pgtoolkit import highlight
16dc5dec9c36 ToolBase: add -d parameter, which triggers debug mode (print SQL queries).
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     4
16dc5dec9c36 ToolBase: add -d parameter, which triggers debug mode (print SQL queries).
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     5
16dc5dec9c36 ToolBase: add -d parameter, which triggers debug mode (print SQL queries).
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     6
class ColoredFormatter(logging.Formatter):
16dc5dec9c36 ToolBase: add -d parameter, which triggers debug mode (print SQL queries).
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     7
    def __init__(self, fmt, datefmt):
16dc5dec9c36 ToolBase: add -d parameter, which triggers debug mode (print SQL queries).
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     8
        logging.Formatter.__init__(self, fmt, datefmt)
16dc5dec9c36 ToolBase: add -d parameter, which triggers debug mode (print SQL queries).
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     9
16dc5dec9c36 ToolBase: add -d parameter, which triggers debug mode (print SQL queries).
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    10
    def format(self, record):
16dc5dec9c36 ToolBase: add -d parameter, which triggers debug mode (print SQL queries).
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    11
        color = ()
16dc5dec9c36 ToolBase: add -d parameter, which triggers debug mode (print SQL queries).
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    12
        if record.levelno == 21:
16dc5dec9c36 ToolBase: add -d parameter, which triggers debug mode (print SQL queries).
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    13
            color = (2, None)
16dc5dec9c36 ToolBase: add -d parameter, which triggers debug mode (print SQL queries).
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    14
        if record.levelno == logging.ERROR and not record.exc_info is None:
16dc5dec9c36 ToolBase: add -d parameter, which triggers debug mode (print SQL queries).
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    15
            color = (11, 1)
16dc5dec9c36 ToolBase: add -d parameter, which triggers debug mode (print SQL queries).
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    16
        if color:
16dc5dec9c36 ToolBase: add -d parameter, which triggers debug mode (print SQL queries).
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    17
            origmsg = record.msg
16dc5dec9c36 ToolBase: add -d parameter, which triggers debug mode (print SQL queries).
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    18
            f,b = color
16dc5dec9c36 ToolBase: add -d parameter, which triggers debug mode (print SQL queries).
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    19
            record.msg = highlight(1, f, b) + record.msg + highlight(0)
16dc5dec9c36 ToolBase: add -d parameter, which triggers debug mode (print SQL queries).
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    20
            res = logging.Formatter.format(self, record)
16dc5dec9c36 ToolBase: add -d parameter, which triggers debug mode (print SQL queries).
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    21
            record.msg = origmsg
16dc5dec9c36 ToolBase: add -d parameter, which triggers debug mode (print SQL queries).
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    22
        else:
16dc5dec9c36 ToolBase: add -d parameter, which triggers debug mode (print SQL queries).
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    23
            res = logging.Formatter.format(self, record)
16dc5dec9c36 ToolBase: add -d parameter, which triggers debug mode (print SQL queries).
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    24
        return res
16dc5dec9c36 ToolBase: add -d parameter, which triggers debug mode (print SQL queries).
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    25