Format the output log
| 25 | |
| 26 | |
| 27 | class _Formatter(logging.Formatter): |
| 28 | ''' |
| 29 | Format the output log |
| 30 | ''' |
| 31 | def __init__(self, colorize=False, *args, **kwargs): |
| 32 | super(_Formatter, self).__init__(*args, **kwargs) |
| 33 | self.colorize = colorize |
| 34 | |
| 35 | @staticmethod |
| 36 | def _process(msg, loglevel, colorize): |
| 37 | loglevel = str(loglevel).lower() |
| 38 | if loglevel not in LOG_LEVELS: |
| 39 | raise RuntimeError(f"{loglevel} should be one of {LOG_LEVELS}." |
| 40 | ) # pragma: no cover |
| 41 | |
| 42 | msg = f"{str(loglevel).upper()}: {str(msg)}" |
| 43 | |
| 44 | if not colorize: |
| 45 | return msg |
| 46 | |
| 47 | if loglevel == DEBUG: |
| 48 | return "{}{}{}".format(fg(5), msg, attr(0)) # noqa: E501 |
| 49 | if loglevel == INFO: |
| 50 | return "{}{}{}".format(fg(4), msg, attr(0)) # noqa: E501 |
| 51 | if loglevel == WARNING: |
| 52 | return "{}{}{}{}{}".format(fg(214), attr(1), msg, attr(21), |
| 53 | attr(0)) # noqa: E501 |
| 54 | if loglevel == ERROR: |
| 55 | return "{}{}{}{}{}".format(fg(202), attr(1), msg, attr(21), |
| 56 | attr(0)) # noqa: E501 |
| 57 | if loglevel == CRITICAL: |
| 58 | return "{}{}{}{}{}".format(fg(196), attr(1), msg, attr(21), |
| 59 | attr(0)) # noqa: E501 |
| 60 | |
| 61 | def format(self, record): |
| 62 | record = copy(record) |
| 63 | loglevel = record.levelname |
| 64 | record.msg = _Formatter._process(record.msg, loglevel, self.colorize) |
| 65 | return super(_Formatter, self).format(record) |
| 66 | |
| 67 | |
| 68 | class Logger: |