Skip to content

Commit

Permalink
Use same code for setting up cli/non-cli formatter
Browse files Browse the repository at this point in the history
A method _create_formatter was introduced that is used for both the
log_cli_formatter and the log_formatter.

Consequences of this commit are:
* Captured logs that are output for each failing test are formatted
  using the ColoredLevelFromatter.
* caplog.text now contains ansi escape sequences.
* The formatter used for writing to a file still uses the non-colored
  logging.Formatter class.
  • Loading branch information
twmr committed May 25, 2019
1 parent 10ca84f commit 96ec0b0
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 17 deletions.
3 changes: 3 additions & 0 deletions changelog/5311.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Captured logs that are output for each failing test are formatted using the
ColoredLevelFromatter. As a consequence caplog.text contains the ANSI
escape sequences used for coloring the level names now.
35 changes: 18 additions & 17 deletions src/_pytest/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ def __init__(self, config):
config.option.verbose = 1

self.print_logs = get_option_ini(config, "log_print")
self.formatter = logging.Formatter(
self.formatter = self._create_formatter(
get_option_ini(config, "log_format"),
get_option_ini(config, "log_date_format"),
)
Expand Down Expand Up @@ -427,6 +427,18 @@ def __init__(self, config):
if self._log_cli_enabled():
self._setup_cli_logging()

def _create_formatter(self, log_format, log_date_format):
if (
self._config.option.color != "no"
and ColoredLevelFormatter.LEVELNAME_FMT_REGEX.search(log_format)
):
formatter = ColoredLevelFormatter(
create_terminal_writer(self._config), log_format, log_date_format
)
else:
formatter = logging.Formatter(log_format, log_date_format)
return formatter

def _setup_cli_logging(self):
config = self._config
terminal_reporter = config.pluginmanager.get_plugin("terminalreporter")
Expand All @@ -437,23 +449,12 @@ def _setup_cli_logging(self):
capture_manager = config.pluginmanager.get_plugin("capturemanager")
# if capturemanager plugin is disabled, live logging still works.
log_cli_handler = _LiveLoggingStreamHandler(terminal_reporter, capture_manager)
log_cli_format = get_option_ini(config, "log_cli_format", "log_format")
log_cli_date_format = get_option_ini(
config, "log_cli_date_format", "log_date_format"

log_cli_formatter = self._create_formatter(
get_option_ini(config, "log_cli_format", "log_format"),
get_option_ini(config, "log_cli_date_format", "log_date_format"),
)
if (
config.option.color != "no"
and ColoredLevelFormatter.LEVELNAME_FMT_REGEX.search(log_cli_format)
):
log_cli_formatter = ColoredLevelFormatter(
create_terminal_writer(config),
log_cli_format,
datefmt=log_cli_date_format,
)
else:
log_cli_formatter = logging.Formatter(
log_cli_format, datefmt=log_cli_date_format
)

log_cli_level = get_actual_log_level(config, "log_cli_level", "log_level")
self.log_cli_handler = log_cli_handler
self.live_logs_context = lambda: catching_logs(
Expand Down

0 comments on commit 96ec0b0

Please sign in to comment.