pgtoolkit/coloredformatter.py
changeset 13 16dc5dec9c36
child 78 64c62ac8f65d
equal deleted inserted replaced
12:203be9022b46 13:16dc5dec9c36
       
     1 import logging
       
     2 
       
     3 from pgtoolkit import highlight
       
     4 
       
     5 
       
     6 class ColoredFormatter(logging.Formatter):
       
     7     def __init__(self, fmt, datefmt):
       
     8         logging.Formatter.__init__(self, fmt, datefmt)
       
     9 
       
    10     def format(self, record):
       
    11         color = ()
       
    12         if record.levelno == 21:
       
    13             color = (2, None)
       
    14         if record.levelno == logging.ERROR and not record.exc_info is None:
       
    15             color = (11, 1)
       
    16         if color:
       
    17             origmsg = record.msg
       
    18             f,b = color
       
    19             record.msg = highlight(1, f, b) + record.msg + highlight(0)
       
    20             res = logging.Formatter.format(self, record)
       
    21             record.msg = origmsg
       
    22         else:
       
    23             res = logging.Formatter.format(self, record)
       
    24         return res
       
    25