Skip to content

Commit bba258a

Browse files
authored
Merge pull request #3190 from s0undt3ch/feature/logs-stream
Expose `log_cli` as a CLI parser option.
2 parents ce0a9aa + a4cbd03 commit bba258a

File tree

3 files changed

+52
-2
lines changed

3 files changed

+52
-2
lines changed

_pytest/logging.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ def __init__(self, config):
338338
self._config = config
339339

340340
# enable verbose output automatically if live logging is enabled
341-
if self._config.getini('log_cli') and not config.getoption('verbose'):
341+
if self._log_cli_enabled() and not config.getoption('verbose'):
342342
# sanity check: terminal reporter should not have been loaded at this point
343343
assert self._config.pluginmanager.get_plugin('terminalreporter') is None
344344
config.option.verbose = 1
@@ -364,6 +364,13 @@ def __init__(self, config):
364364
# initialized during pytest_runtestloop
365365
self.log_cli_handler = None
366366

367+
def _log_cli_enabled(self):
368+
"""Return True if log_cli should be considered enabled, either explicitly
369+
or because --log-cli-level was given in the command-line.
370+
"""
371+
return self._config.getoption('--log-cli-level') is not None or \
372+
self._config.getini('log_cli')
373+
367374
@contextmanager
368375
def _runtest_for(self, item, when):
369376
"""Implements the internals of pytest_runtest_xxx() hook."""
@@ -438,7 +445,7 @@ def _setup_cli_logging(self):
438445
This must be done right before starting the loop so we can access the terminal reporter plugin.
439446
"""
440447
terminal_reporter = self._config.pluginmanager.get_plugin('terminalreporter')
441-
if self._config.getini('log_cli') and terminal_reporter is not None:
448+
if self._log_cli_enabled() and terminal_reporter is not None:
442449
capture_manager = self._config.pluginmanager.get_plugin('capturemanager')
443450
log_cli_handler = _LiveLoggingStreamHandler(terminal_reporter, capture_manager)
444451
log_cli_format = get_option_ini(self._config, 'log_cli_format', 'log_format')

changelog/3190.feature

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Passing `--log-cli-level` in the command-line now automatically activates live logging.

testing/logging/test_reporting.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,48 @@ def test_log_cli(request):
366366
assert result.ret == 0
367367

368368

369+
@pytest.mark.parametrize('cli_args', ['',
370+
'--log-level=WARNING',
371+
'--log-file-level=WARNING',
372+
'--log-cli-level=WARNING'])
373+
def test_log_cli_auto_enable(testdir, request, cli_args):
374+
"""Check that live logs are enabled if --log-level or --log-cli-level is passed on the CLI.
375+
It should not be auto enabled if the same configs are set on the INI file.
376+
"""
377+
testdir.makepyfile('''
378+
import pytest
379+
import logging
380+
381+
def test_log_1():
382+
logging.info("log message from test_log_1 not to be shown")
383+
logging.warning("log message from test_log_1")
384+
385+
''')
386+
testdir.makeini('''
387+
[pytest]
388+
log_level=INFO
389+
log_cli_level=INFO
390+
''')
391+
392+
result = testdir.runpytest(cli_args)
393+
if cli_args == '--log-cli-level=WARNING':
394+
result.stdout.fnmatch_lines([
395+
'*::test_log_1 ',
396+
'*-- live log call --*',
397+
'*WARNING*log message from test_log_1*',
398+
'PASSED *100%*',
399+
'=* 1 passed in *=',
400+
])
401+
assert 'INFO' not in result.stdout.str()
402+
else:
403+
result.stdout.fnmatch_lines([
404+
'*test_log_cli_auto_enable*100%*',
405+
'=* 1 passed in *=',
406+
])
407+
assert 'INFO' not in result.stdout.str()
408+
assert 'WARNING' not in result.stdout.str()
409+
410+
369411
def test_log_file_cli(testdir):
370412
# Default log file level
371413
testdir.makepyfile('''

0 commit comments

Comments
 (0)