Skip to content

Commit

Permalink
log_cli must now be enabled explicitly
Browse files Browse the repository at this point in the history
  • Loading branch information
nicoddemus committed Jan 15, 2018
1 parent 01e37fe commit 58e6452
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 18 deletions.
46 changes: 28 additions & 18 deletions _pytest/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ 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-level',
dest='log_cli_level', default=None,
Expand Down Expand Up @@ -227,8 +230,12 @@ def get_actual_log_level(config, *setting_names):


def pytest_configure(config):
config.pluginmanager.register(LoggingPlugin(config),
'logging-plugin')
config.pluginmanager.register(LoggingPlugin(config), 'logging-plugin')


@contextmanager
def _dummy_context_manager():
yield


class LoggingPlugin(object):
Expand All @@ -241,26 +248,29 @@ def __init__(self, config):
The formatter can be safely shared across all handlers so
create a single one for the entire test session here.
"""
self.log_cli_level = get_actual_log_level(
config, 'log_cli_level', 'log_level') or logging.WARNING

self.print_logs = get_option_ini(config, 'log_print')
self.formatter = logging.Formatter(
get_option_ini(config, 'log_format'),
get_option_ini(config, 'log_date_format'))

log_cli_handler = logging.StreamHandler(sys.stderr)
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 = logging.Formatter(
log_cli_format,
datefmt=log_cli_date_format)
self.log_cli_handler = log_cli_handler # needed for a single unittest
self.live_logs = catching_logs(log_cli_handler,
formatter=log_cli_formatter,
level=self.log_cli_level)
if config.getini('log_cli'):
log_cli_handler = logging.StreamHandler(sys.stderr)
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 = logging.Formatter(
log_cli_format,
datefmt=log_cli_date_format)
log_cli_level = get_actual_log_level(
config, 'log_cli_level', 'log_level') or logging.WARNING
self.log_cli_handler = log_cli_handler # needed for a single unittest
self.live_logs_context = catching_logs(log_cli_handler,
formatter=log_cli_formatter,
level=log_cli_level)
else:
self.log_cli_handler = None
self.live_logs_context = _dummy_context_manager()

log_file = get_option_ini(config, 'log_file')
if log_file:
Expand Down Expand Up @@ -316,7 +326,7 @@ def pytest_runtest_teardown(self, item):
@pytest.hookimpl(hookwrapper=True)
def pytest_runtestloop(self, session):
"""Runs all collected test items."""
with self.live_logs:
with self.live_logs_context:
if self.log_file_handler is not None:
with closing(self.log_file_handler):
with catching_logs(self.log_file_handler,
Expand Down
29 changes: 29 additions & 0 deletions testing/logging/test_reporting.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,26 @@ def test_foo():
result.stdout.fnmatch_lines(['*- Captured *log call -*'])


@pytest.mark.parametrize('enabled', [True, False])
def test_log_cli_enabled_disabled(testdir, enabled):
msg = 'critical message logged by test'
testdir.makepyfile('''
import logging
def test_log_cli():
logging.critical("{}")
'''.format(msg))
if enabled:
testdir.makeini('''
[pytest]
log_cli=true
''')
result = testdir.runpytest('-s')
if enabled:
assert msg in result.stderr.str()
else:
assert msg not in result.stderr.str()


def test_log_cli_default_level(testdir):
# Default log file level
testdir.makepyfile('''
Expand All @@ -153,6 +173,10 @@ def test_log_cli(request):
logging.getLogger('catchlog').warning("This log message will be shown")
print('PASSED')
''')
testdir.makeini('''
[pytest]
log_cli=true
''')

result = testdir.runpytest('-s')

Expand Down Expand Up @@ -186,6 +210,10 @@ def test_log_cli(request):
logging.getLogger('catchlog').info("This log message will be shown")
print('PASSED')
''')
testdir.makeini('''
[pytest]
log_cli=true
''')

result = testdir.runpytest('-s', '--log-cli-level=INFO')

Expand Down Expand Up @@ -230,6 +258,7 @@ def test_log_cli_ini_level(testdir):
testdir.makeini(
"""
[pytest]
log_cli=true
log_cli_level = INFO
""")
testdir.makepyfile('''
Expand Down

0 comments on commit 58e6452

Please sign in to comment.