|
2 | 2 |
|
3 | 3 | import logging |
4 | 4 | from contextlib import closing, contextmanager |
| 5 | +import re |
5 | 6 | import six |
6 | 7 |
|
| 8 | +from _pytest.config import create_terminal_writer |
7 | 9 | import pytest |
8 | 10 | import py |
9 | 11 |
|
|
12 | 14 | DEFAULT_LOG_DATE_FORMAT = '%H:%M:%S' |
13 | 15 |
|
14 | 16 |
|
| 17 | +class ColoredLevelFormatter(logging.Formatter): |
| 18 | + """ |
| 19 | + Colorize the %(levelname)..s part of the log format passed to __init__. |
| 20 | + """ |
| 21 | + |
| 22 | + LOGLEVEL_COLOROPTS = { |
| 23 | + logging.CRITICAL: {'red'}, |
| 24 | + logging.ERROR: {'red', 'bold'}, |
| 25 | + logging.WARNING: {'yellow'}, |
| 26 | + logging.WARN: {'yellow'}, |
| 27 | + logging.INFO: {'green'}, |
| 28 | + logging.DEBUG: {'purple'}, |
| 29 | + logging.NOTSET: set(), |
| 30 | + } |
| 31 | + LEVELNAME_FMT_REGEX = re.compile(r'%\(levelname\)([+-]?\d*s)') |
| 32 | + |
| 33 | + def __init__(self, terminalwriter, *args, **kwargs): |
| 34 | + super(ColoredLevelFormatter, self).__init__( |
| 35 | + *args, **kwargs) |
| 36 | + if six.PY2: |
| 37 | + self._original_fmt = self._fmt |
| 38 | + else: |
| 39 | + self._original_fmt = self._style._fmt |
| 40 | + self._level_to_fmt_mapping = {} |
| 41 | + |
| 42 | + levelname_fmt_match = self.LEVELNAME_FMT_REGEX.search(self._fmt) |
| 43 | + if not levelname_fmt_match: |
| 44 | + return |
| 45 | + levelname_fmt = levelname_fmt_match.group() |
| 46 | + |
| 47 | + for level, color_opts in self.LOGLEVEL_COLOROPTS.items(): |
| 48 | + formatted_levelname = levelname_fmt % { |
| 49 | + 'levelname': logging.getLevelName(level)} |
| 50 | + |
| 51 | + # add ANSI escape sequences around the formatted levelname |
| 52 | + color_kwargs = {name: True for name in color_opts} |
| 53 | + colorized_formatted_levelname = terminalwriter.markup( |
| 54 | + formatted_levelname, **color_kwargs) |
| 55 | + self._level_to_fmt_mapping[level] = self.LEVELNAME_FMT_REGEX.sub( |
| 56 | + colorized_formatted_levelname, |
| 57 | + self._fmt) |
| 58 | + |
| 59 | + def format(self, record): |
| 60 | + fmt = self._level_to_fmt_mapping.get( |
| 61 | + record.levelno, self._original_fmt) |
| 62 | + if six.PY2: |
| 63 | + self._fmt = fmt |
| 64 | + else: |
| 65 | + self._style._fmt = fmt |
| 66 | + return super(ColoredLevelFormatter, self).format(record) |
| 67 | + |
| 68 | + |
15 | 69 | def get_option_ini(config, *names): |
16 | 70 | for name in names: |
17 | 71 | ret = config.getoption(name) # 'default' arg won't work as expected |
@@ -376,7 +430,11 @@ def _setup_cli_logging(self): |
376 | 430 | log_cli_handler = _LiveLoggingStreamHandler(terminal_reporter, capture_manager) |
377 | 431 | log_cli_format = get_option_ini(self._config, 'log_cli_format', 'log_format') |
378 | 432 | log_cli_date_format = get_option_ini(self._config, 'log_cli_date_format', 'log_date_format') |
379 | | - log_cli_formatter = logging.Formatter(log_cli_format, datefmt=log_cli_date_format) |
| 433 | + if self._config.option.color != 'no' and ColoredLevelFormatter.LEVELNAME_FMT_REGEX.search(log_cli_format): |
| 434 | + log_cli_formatter = ColoredLevelFormatter(create_terminal_writer(self._config), |
| 435 | + log_cli_format, datefmt=log_cli_date_format) |
| 436 | + else: |
| 437 | + log_cli_formatter = logging.Formatter(log_cli_format, datefmt=log_cli_date_format) |
380 | 438 | log_cli_level = get_actual_log_level(self._config, 'log_cli_level', 'log_level') |
381 | 439 | self.log_cli_handler = log_cli_handler |
382 | 440 | self.live_logs_context = catching_logs(log_cli_handler, formatter=log_cli_formatter, level=log_cli_level) |
|
0 commit comments