Changeset 190

Show
Ignore:
Timestamp:
08/13/08 20:14:52 (4 months ago)
Author:
apdavison
Message:

Added some stuff to make logging easier to setup (NeuroTools.init_logging(filename)) and with fancier formatting (colours, indents), to make it easier to read on the console.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/src/__init__.py

    r183 r190  
    99check_pytables_version() 
    1010""" 
     11 
     12# Setup fancy logging 
     13 
     14red     = 0010; green  = 0020; yellow = 0030; blue = 0040; 
     15magenta = 0050; cyan   = 0060; bright = 0100 
     16try: 
     17    import ll.ansistyle 
     18    def colour(col,text): 
     19        return str(ll.ansistyle.Text(col,str(text))) 
     20except ImportError: 
     21    def colour(col,text): 
     22            return text 
     23         
     24import logging 
     25 
     26# Add a header() level to logging 
     27logging.HEADER = 60 
     28logging.addLevelName(logging.HEADER, 'HEADER') 
     29 
     30root = logging.getLogger() 
     31 
     32def root_header(msg, *args, **kwargs): 
     33    if len(root.handlers) == 0: 
     34        basicConfig() 
     35    apply(root.header, (msg,)+args, kwargs) 
     36 
     37def logger_header(self, msg, *args, **kwargs): 
     38    if self.manager.disable >= logging.HEADER: 
     39        return 
     40    if logging.HEADER >= self.getEffectiveLevel(): 
     41        apply(self._log, (logging.HEADER, msg, args), kwargs) 
     42 
     43logging.Logger.header = logger_header 
     44logging.header = root_header 
     45 
     46class FancyFormatter(logging.Formatter): 
     47    """ 
     48    A log formatter that colours and indents the log message depending on the level. 
     49    """ 
     50     
     51    DEFAULT_COLOURS = { 
     52        'CRITICAL': bright+red, 
     53        'ERROR': red, 
     54        'WARNING': magenta, 
     55        'HEADER': bright+yellow, 
     56        'INFO': cyan, 
     57        'DEBUG': green 
     58    } 
     59     
     60    DEFAULT_INDENTS = { 
     61        'CRITICAL': "", 
     62        'ERROR': "", 
     63        'WARNING': "", 
     64        'HEADER': "", 
     65        'INFO': "  ", 
     66        'DEBUG': "    ", 
     67    } 
     68     
     69    def __init__(self, fmt=None, datefmt=None, colours=DEFAULT_COLOURS): 
     70        logging.Formatter.__init__(self, fmt, datefmt) 
     71        self._colours = colours 
     72        self._indents = FancyFormatter.DEFAULT_INDENTS 
     73     
     74    def format(self, record): 
     75        s = logging.Formatter.format(self, record) 
     76        if record.levelname == "HEADER": 
     77            s = "=== %s ===" % s 
     78        if self._colours: 
     79            s = colour(self._colours[record.levelname], s) 
     80        return self._indents[record.levelname] + s 
     81 
     82 
     83def init_logging(filename, file_level=logging.INFO, console_level=logging.WARNING): 
     84    logging.basicConfig(level=file_level, 
     85                        format='%(asctime)s %(levelname)s %(message)s', # %(pathname)s %(module)s %(funcName)s', 
     86                        filename=filename, 
     87                        filemode='w') 
     88    console = logging.StreamHandler() 
     89    console.setLevel(console_level) 
     90    console.setFormatter(FancyFormatter('%(message)s')) 
     91    logging.getLogger('').addHandler(console)