| 23 | |
| 24 | class _ColorizingStreamHandler(ColorizingStreamHandler): |
| 25 | def colorize(self, message, levelno, force=False): |
| 26 | if levelno in self.level_map and (self.is_tty or force): |
| 27 | bg, fg, bold = self.level_map[levelno] |
| 28 | params = [] |
| 29 | |
| 30 | if bg in self.color_map: |
| 31 | params.append(str(self.color_map[bg] + 40)) |
| 32 | |
| 33 | if fg in self.color_map: |
| 34 | params.append(str(self.color_map[fg] + 30)) |
| 35 | |
| 36 | if bold: |
| 37 | params.append('1') |
| 38 | |
| 39 | if params and message: |
| 40 | match = re.search(r"\A(\s+)", message) |
| 41 | prefix = match.group(1) if match else "" |
| 42 | message = message[len(prefix):] |
| 43 | |
| 44 | match = re.search(r"\[([A-Z ]+)\]", message) # log level |
| 45 | if match: |
| 46 | level = match.group(1) |
| 47 | if message.startswith(self.bold): |
| 48 | message = message.replace(self.bold, "") |
| 49 | reset = self.reset + self.bold |
| 50 | params.append('1') |
| 51 | else: |
| 52 | reset = self.reset |
| 53 | message = message.replace(level, ''.join((self.csi, ';'.join(params), 'm', level, reset)), 1) |
| 54 | |
| 55 | match = re.search(r"\A\s*\[([\d:]+)\]", message) # time |
| 56 | if match: |
| 57 | time = match.group(1) |
| 58 | message = message.replace(time, ''.join((self.csi, str(self.color_map["cyan"] + 30), 'm', time, self._reset(message))), 1) |
| 59 | |
| 60 | match = re.search(r"\[(#\d+)\]", message) # counter |
| 61 | if match: |
| 62 | counter = match.group(1) |
| 63 | message = message.replace(counter, ''.join((self.csi, str(self.color_map["yellow"] + 30), 'm', counter, self._reset(message))), 1) |
| 64 | |
| 65 | if level != "PAYLOAD": |
| 66 | if any(_ in message for _ in ("parsed DBMS error message",)): |
| 67 | match = re.search(r": '(.+)'", message) |
| 68 | if match: |
| 69 | string = match.group(1) |
| 70 | message = message.replace("'%s'" % string, "'%s'" % ''.join((self.csi, str(self.color_map["white"] + 30), 'm', string, self._reset(message))), 1) |
| 71 | else: |
| 72 | match = re.search(r"\bresumed: '(.+\.\.\.)", message) |
| 73 | if match: |
| 74 | string = match.group(1) |
| 75 | message = message.replace("'%s" % string, "'%s" % ''.join((self.csi, str(self.color_map["white"] + 30), 'm', string, self._reset(message))), 1) |
| 76 | else: |
| 77 | match = re.search(r" \('(.+)'\)\Z", message) or re.search(r"output: '(.+)'\Z", message) |
| 78 | if match: |
| 79 | string = match.group(1) |
| 80 | message = message.replace("'%s'" % string, "'%s'" % ''.join((self.csi, str(self.color_map["white"] + 30), 'm', string, self._reset(message))), 1) |
| 81 | else: |
| 82 | for match in re.finditer(r"[^\w]'([^']+)'", message): # single-quoted |