Skip to content

Commit

Permalink
Expose log_cli as a CLI parser option.
Browse files Browse the repository at this point in the history
  • Loading branch information
s0undt3ch committed Feb 6, 2018
1 parent f2fb841 commit 5dfb172
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 5 deletions.
22 changes: 17 additions & 5 deletions _pytest/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,14 @@ def add_option_ini(option, dest, default=None, type=None, **kwargs):
'--log-date-format',
dest='log_date_format', default=DEFAULT_LOG_DATE_FORMAT,
help='log date format as used by the logging module.')
parser.addini(
'log_cli', default=False, type='bool',
help='enable log display during test run (also known as "live logging").')
add_option_ini(
'--log-cli',
dest='log_cli',
default=False, action='store_true',
type='bool',
help='enable log display during test run (also known as '
'"live logging").'
)
add_option_ini(
'--log-cli-level',
dest='log_cli_level', default=None,
Expand Down Expand Up @@ -337,8 +342,14 @@ def __init__(self, config):
"""
self._config = config

auto_enable_log_stream = self._config.getoption('--log-cli-level')
if auto_enable_log_stream is not None:
# Explicitely enable log_cli is --log-cli-level was passed on the CLI
# It won't be automatically enabled is set on the ini file.
config._inicache['log_cli'] = config._parser._inidict['log_cli'] = True
# enable verbose output automatically if live logging is enabled
if self._config.getini('log_cli') and not config.getoption('verbose'):
stream_logs = self._config.getoption('log_cli') or self._config.getini('log_cli')
if stream_logs is True and not config.getoption('verbose'):
# sanity check: terminal reporter should not have been loaded at this point
assert self._config.pluginmanager.get_plugin('terminalreporter') is None
config.option.verbose = 1
Expand Down Expand Up @@ -425,7 +436,8 @@ def _setup_cli_logging(self):
This must be done right before starting the loop so we can access the terminal reporter plugin.
"""
terminal_reporter = self._config.pluginmanager.get_plugin('terminalreporter')
if self._config.getini('log_cli') and terminal_reporter is not None:
stream_logs = self._config.getoption('log_cli') or self._config.getini('log_cli')
if stream_logs and terminal_reporter is not None:
capture_manager = self._config.pluginmanager.get_plugin('capturemanager')
log_cli_handler = _LiveLoggingStreamHandler(terminal_reporter, capture_manager)
log_cli_format = get_option_ini(self._config, 'log_cli_format', 'log_format')
Expand Down
2 changes: 2 additions & 0 deletions changelog/3190.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Expose `log_cli` as a CLI parser option.
Enable live logs if `--log-cli-level` is passed. Setting `log_cli_level` on the INI file has no effect.
50 changes: 50 additions & 0 deletions testing/logging/test_reporting.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,56 @@ def test_log_cli(request):
assert result.ret == 0


@pytest.mark.parametrize('cli_args', ['',
'--log-stream',
'--log-level=WARNING',
'--log-file-level=WARNING',
'--log-cli-level=WARNING'])
def test_log_cli_auto_enable(testdir, request, cli_args):
"""Check that live logs are enabled if --log-level or --log-cli-level is passed on the CLI.
It should not be auto enabled if the same configs are set on the INI file.
"""
testdir.makepyfile('''
import pytest
import logging
def test_log_1():
logging.info("log message from test_log_1 not to be shown")
logging.warning("log message from test_log_1")
''')
testdir.makeini('''
[pytest]
log_level=INFO
log_cli_level=INFO
''')

result = testdir.runpytest(cli_args)
if cli_args == '--log-cli-level=WARNING':
result.stdout.fnmatch_lines([
'*::test_log_1 ',
'*-- live log call --*',
'*WARNING*log message from test_log_1*',
'PASSED *100%*',
'=* 1 passed in *=',
])
assert 'INFO' not in result.stdout.str()
elif cli_args == '--log-stream':
result.stdout.fnmatch_lines([
'*::test_log_1 ',
'*-- live log call --*',
'*INFO*log message from test_log_1 not to be shown*',
'*WARNING*log message from test_log_1*',
'PASSED *100%*',
'=* 1 passed in *=',
])
else:
result.stdout.fnmatch_lines([
'*test_log_cli_auto_enable*100%*',
'=* 1 passed in *=',
])


def test_log_file_cli(testdir):
# Default log file level
testdir.makepyfile('''
Expand Down

0 comments on commit 5dfb172

Please sign in to comment.