Trees | Indices | Help |
---|
|
1 import logging 2 # method to add color to a logging.info add a second argument: 3 # '$MG:color:BLACK' 4 5 6 BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE = range(8) 7 8 COLORS = { 9 'WARNING' : BLUE, 10 'INFO' : BLACK, 11 'DEBUG' : GREEN, 12 'CRITICAL' : RED, 13 'ERROR' : RED, 14 'BLACK' : BLACK, 15 'RED' : RED, 16 'GREEN' : GREEN, 17 'YELLOW' : YELLOW, 18 'BLUE' : BLUE, 19 'MAGENTA' : MAGENTA, 20 'CYAN' : CYAN, 21 'WHITE' : WHITE, 22 } 23 24 for i in range(0,11): 25 COLORS['Level %i'%i] = COLORS['DEBUG'] 26 27 RESET_SEQ = "\033[0m" 28 COLOR_SEQ = "\033[1;%dm" 29 BOLD_SEQ = "\033[1m" 303280 81 logging.ColorFormatter = ColorFormatter 8234 # can't do super(...) here because Formatter is an old school class 35 logging.Formatter.__init__(self, *args, **kwargs)3638 levelname = record.levelname 39 try: 40 color_choice = COLORS[levelname] 41 except KeyError: 42 color_choice = COLORS['INFO'] 43 new_args=[] 44 # A not-so-nice but working way of passing arguments to this formatter 45 # from MadGraph. 46 color_specified = False 47 for arg in record.args: 48 if isinstance(arg,str) and arg.startswith('$MG'): 49 elems=arg.split(':') 50 if len(elems)>2: 51 if elems[1]=='color': 52 color_specified = True 53 color_choice = COLORS[elems[2]] 54 if color_choice == 0: 55 color_choice = 30 56 else: 57 new_args.append(arg) 58 record.args = tuple(new_args) 59 color = COLOR_SEQ % (30 + color_choice) 60 message = logging.Formatter.format(self, record) 61 if not message.endswith('$RESET'): 62 message += '$RESET' 63 for k,v in COLORS.items(): 64 color_flag = COLOR_SEQ % (v+30) 65 message = message.replace("$" + k, color_flag)\ 66 .replace("$BG" + k, COLOR_SEQ % (v+40))\ 67 .replace("$BG-" + k, COLOR_SEQ % (v+40)) 68 69 if levelname == 'INFO': 70 message = message.replace("$RESET", '' if not color_specified else RESET_SEQ)\ 71 .replace("$BOLD", '')\ 72 .replace("$COLOR", color if color_specified else '') 73 return message 74 else: 75 message = message.replace("$RESET", RESET_SEQ)\ 76 .replace("$BOLD", BOLD_SEQ)\ 77 .replace("$COLOR", color) 78 79 return message
Trees | Indices | Help |
---|
Generated by Epydoc 3.0.1 on Fri Jun 10 10:40:26 2016 | http://epydoc.sourceforge.net |