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