https://stackoverflow.com/questions/6290739/python-logging-use-milliseconds-in-time-format
| 6 | |
| 7 | |
| 8 | class MyFormatter(logging.Formatter): |
| 9 | """ |
| 10 | https://stackoverflow.com/questions/6290739/python-logging-use-milliseconds-in-time-format |
| 11 | """ |
| 12 | |
| 13 | def formatTime(self, record, datefmt=None): |
| 14 | ct = datetime.fromtimestamp(record.created) |
| 15 | if datefmt: |
| 16 | s = ct.strftime(datefmt) |
| 17 | else: |
| 18 | t = ct.strftime("%Y-%m-%d %H:%M:%S") |
| 19 | s = "%s.%03d" % (t, record.msecs) |
| 20 | return s |
| 21 | |
| 22 | def format(self, record): |
| 23 | level_name = record.levelname |
| 24 | if level_name == 'ERROR': |
| 25 | color = 31 |
| 26 | elif level_name == 'WARNING': |
| 27 | color = 33 |
| 28 | elif level_name == 'INFO': |
| 29 | color = 32 |
| 30 | else: |
| 31 | color = 34 |
| 32 | while len(level_name) < 7: |
| 33 | level_name += ' ' |
| 34 | record.levelname = '\033[{0}m{1}\033[0m'.format(color, level_name) |
| 35 | return logging.Formatter.format(self, record) |
| 36 | |
| 37 | |
| 38 | def init_log(): |