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