Skip to content

Commit 7b8fd0c

Browse files
committed
Refactor _setup_cli_logging code
Change the indentation in _setup_cli_logging by moving the self._log_cli_enabled check outside of the _setup_cli_logging method.
1 parent 429485e commit 7b8fd0c

File tree

1 file changed

+43
-44
lines changed

1 file changed

+43
-44
lines changed

src/_pytest/logging.py

Lines changed: 43 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -420,54 +420,53 @@ def __init__(self, config):
420420

421421
self.log_cli_handler = None
422422

423-
self._setup_cli_logging()
423+
self.live_logs_context = lambda: dummy_context_manager()
424+
# Note that the lambda for the live_logs_context is needed because
425+
# live_logs_context can otherwise not be entered multiple times due
426+
# to limitations of contextlib.contextmanager.
427+
428+
if self._log_cli_enabled():
429+
self._setup_cli_logging()
424430

425431
def _setup_cli_logging(self):
426-
terminal_reporter = self._config.pluginmanager.get_plugin("terminalreporter")
427-
428-
if self._log_cli_enabled() and terminal_reporter is not None:
429-
# FIXME don't set verbosity level and derived attributes of
430-
# terminalwriter directly
431-
terminal_reporter.verbosity = self._config.option.verbose
432-
terminal_reporter.showheader = terminal_reporter.verbosity >= 0
433-
terminal_reporter.showfspath = terminal_reporter.verbosity >= 0
434-
terminal_reporter.showlongtestinfo = terminal_reporter.verbosity > 0
435-
436-
capture_manager = self._config.pluginmanager.get_plugin("capturemanager")
437-
log_cli_handler = _LiveLoggingStreamHandler(
438-
terminal_reporter, capture_manager
439-
)
440-
log_cli_format = get_option_ini(
441-
self._config, "log_cli_format", "log_format"
442-
)
443-
log_cli_date_format = get_option_ini(
444-
self._config, "log_cli_date_format", "log_date_format"
445-
)
446-
if (
447-
self._config.option.color != "no"
448-
and ColoredLevelFormatter.LEVELNAME_FMT_REGEX.search(log_cli_format)
449-
):
450-
log_cli_formatter = ColoredLevelFormatter(
451-
create_terminal_writer(self._config),
452-
log_cli_format,
453-
datefmt=log_cli_date_format,
454-
)
455-
else:
456-
log_cli_formatter = logging.Formatter(
457-
log_cli_format, datefmt=log_cli_date_format
458-
)
459-
log_cli_level = get_actual_log_level(
460-
self._config, "log_cli_level", "log_level"
461-
)
462-
self.log_cli_handler = log_cli_handler
463-
self.live_logs_context = lambda: catching_logs(
464-
log_cli_handler, formatter=log_cli_formatter, level=log_cli_level
432+
config = self._config
433+
terminal_reporter = config.pluginmanager.get_plugin("terminalreporter")
434+
if terminal_reporter is None:
435+
# terminal reporter is disabled e.g. by pytest-xdist.
436+
return
437+
438+
# FIXME don't set verbosity level and derived attributes of
439+
# terminalwriter directly
440+
terminal_reporter.verbosity = config.option.verbose
441+
terminal_reporter.showheader = terminal_reporter.verbosity >= 0
442+
terminal_reporter.showfspath = terminal_reporter.verbosity >= 0
443+
terminal_reporter.showlongtestinfo = terminal_reporter.verbosity > 0
444+
445+
capture_manager = config.pluginmanager.get_plugin("capturemanager")
446+
# if capturemanager plugin is disabled, live logging still works.
447+
log_cli_handler = _LiveLoggingStreamHandler(terminal_reporter, capture_manager)
448+
log_cli_format = get_option_ini(config, "log_cli_format", "log_format")
449+
log_cli_date_format = get_option_ini(
450+
config, "log_cli_date_format", "log_date_format"
451+
)
452+
if (
453+
config.option.color != "no"
454+
and ColoredLevelFormatter.LEVELNAME_FMT_REGEX.search(log_cli_format)
455+
):
456+
log_cli_formatter = ColoredLevelFormatter(
457+
create_terminal_writer(config),
458+
log_cli_format,
459+
datefmt=log_cli_date_format,
465460
)
466461
else:
467-
self.live_logs_context = lambda: dummy_context_manager()
468-
# Note that the lambda for the live_logs_context is needed because
469-
# live_logs_context can otherwise not be entered multiple times due
470-
# to limitations of contextlib.contextmanager
462+
log_cli_formatter = logging.Formatter(
463+
log_cli_format, datefmt=log_cli_date_format
464+
)
465+
log_cli_level = get_actual_log_level(config, "log_cli_level", "log_level")
466+
self.log_cli_handler = log_cli_handler
467+
self.live_logs_context = lambda: catching_logs(
468+
log_cli_handler, formatter=log_cli_formatter, level=log_cli_level
469+
)
471470

472471
def _log_cli_enabled(self):
473472
"""Return True if log_cli should be considered enabled, either explicitly

0 commit comments

Comments
 (0)