<s> import logging <EOL> class ColorFormatter ( logging . Formatter ) : <EOL> """<STR_LIT>""" <EOL> color_end = "<STR_LIT>" <EOL> colors = dict ( <EOL> default = '<STR_LIT:0>' , <EOL> grey = '<STR_LIT>' , <EOL> green = "<STR_LIT>" , <EOL> red = "<STR_LIT>" , <EOL> yellow = "<STR_LIT>" , <EOL> purple = "<STR_LIT>" , <EOL> cyan = "<STR_LIT>" <EOL> ) <EOL> level_map = { <EOL> logging . DEBUG : ( '<STR_LIT>' , False ) , <EOL> logging . INFO : ( '<STR_LIT>' , False ) , <EOL> logging . WARNING : ( '<STR_LIT>' , False ) , <EOL> logging . ERROR : ( '<STR_LIT>' , False ) , <EOL> logging . CRITICAL : ( '<STR_LIT>' , True ) <EOL> } <EOL> def __init__ ( self , fmt = None , datefmt = None ) : <EOL> super ( ColorFormatter , self ) . __init__ ( fmt , datefmt ) <EOL> def format ( self , record ) : <EOL> lvl = int ( record . levelno / <NUM_LIT:10> ) * <NUM_LIT:10> <EOL> record . msg = self . _colorize ( self . level_map . get ( lvl ) [ <NUM_LIT:0> ] , record . msg , self . level_map . get ( lvl ) [ <NUM_LIT:1> ] ) <EOL> return super ( ColorFormatter , self ) . format ( record ) <EOL> def _colorize ( self , color , text , bold = False ) : <EOL> """<STR_LIT>""" <EOL> bold = <NUM_LIT:1> if bold else <NUM_LIT:0> <EOL> return "<STR_LIT>" % ( bold , self . colors . get ( color , self . colors [ '<STR_LIT:default>' ] ) , text , self . color_end ) <EOL> class LevelFilter ( logging . Filter ) : <EOL> def __init__ ( self , name = '<STR_LIT>' , level = logging . DEBUG ) : <EOL> super ( LevelFilter , self ) . __init__ ( name ) <EOL> self . _level = level <EOL> def filter ( self , record ) : <EOL> return int ( record . levelno / <NUM_LIT:10> ) * <NUM_LIT:10> == self . _level <EOL> _LOGGING_FMT = "<STR_LIT>" <EOL> LOGME = logging . getLogger ( __name__ ) <EOL> LOGME . setLevel ( logging . DEBUG ) <EOL> HANDLER = logging . StreamHandler ( ) <EOL> HANDLER . setLevel ( logging . INFO ) <EOL> HANDLER . setFormatter ( ColorFormatter ( fmt = _LOGGING_FMT ) ) <EOL> LOGME . addHandler ( HANDLER ) <EOL> def add_verbose_handler ( ) : <EOL> """<STR_LIT>""" <EOL> verbose_handler = logging . StreamHandler ( ) <EOL> verbose_handler . setLevel ( logging . DEBUG ) <EOL> verbose_handler . setFormatter ( ColorFormatter ( fmt = _LOGGING_FMT ) ) <EOL> verbose_handler . addFilter ( LevelFilter ( level = verbose_handler . level ) ) <EOL> LOGME . addHandler ( verbose_handler ) </s>
