Skip to content

Commit 5ad1313

Browse files
committed
log_cli must now be enabled explicitly
Ref: pytest-dev#3013
1 parent 3b3d237 commit 5ad1313

File tree

2 files changed

+57
-18
lines changed

2 files changed

+57
-18
lines changed

_pytest/logging.py

+28-18
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ def add_option_ini(option, dest, default=None, type=None, **kwargs):
4848
'--log-date-format',
4949
dest='log_date_format', default=DEFAULT_LOG_DATE_FORMAT,
5050
help='log date format as used by the logging module.')
51+
parser.addini(
52+
'log_cli', default=False, type='bool',
53+
help='enable log display during test run (also known as "live logging").')
5154
add_option_ini(
5255
'--log-cli-level',
5356
dest='log_cli_level', default=None,
@@ -234,8 +237,12 @@ def get_actual_log_level(config, *setting_names):
234237

235238

236239
def pytest_configure(config):
237-
config.pluginmanager.register(LoggingPlugin(config),
238-
'logging-plugin')
240+
config.pluginmanager.register(LoggingPlugin(config), 'logging-plugin')
241+
242+
243+
@contextmanager
244+
def _dummy_context_manager():
245+
yield
239246

240247

241248
class LoggingPlugin(object):
@@ -248,26 +255,29 @@ def __init__(self, config):
248255
The formatter can be safely shared across all handlers so
249256
create a single one for the entire test session here.
250257
"""
251-
self.log_cli_level = get_actual_log_level(
252-
config, 'log_cli_level', 'log_level') or logging.WARNING
253-
254258
self.print_logs = get_option_ini(config, 'log_print')
255259
self.formatter = logging.Formatter(
256260
get_option_ini(config, 'log_format'),
257261
get_option_ini(config, 'log_date_format'))
258262

259-
log_cli_handler = logging.StreamHandler(sys.stderr)
260-
log_cli_format = get_option_ini(
261-
config, 'log_cli_format', 'log_format')
262-
log_cli_date_format = get_option_ini(
263-
config, 'log_cli_date_format', 'log_date_format')
264-
log_cli_formatter = logging.Formatter(
265-
log_cli_format,
266-
datefmt=log_cli_date_format)
267-
self.log_cli_handler = log_cli_handler # needed for a single unittest
268-
self.live_logs = catching_logs(log_cli_handler,
269-
formatter=log_cli_formatter,
270-
level=self.log_cli_level)
263+
if config.getini('log_cli'):
264+
log_cli_handler = logging.StreamHandler(sys.stderr)
265+
log_cli_format = get_option_ini(
266+
config, 'log_cli_format', 'log_format')
267+
log_cli_date_format = get_option_ini(
268+
config, 'log_cli_date_format', 'log_date_format')
269+
log_cli_formatter = logging.Formatter(
270+
log_cli_format,
271+
datefmt=log_cli_date_format)
272+
log_cli_level = get_actual_log_level(
273+
config, 'log_cli_level', 'log_level') or logging.WARNING
274+
self.log_cli_handler = log_cli_handler # needed for a single unittest
275+
self.live_logs_context = catching_logs(log_cli_handler,
276+
formatter=log_cli_formatter,
277+
level=log_cli_level)
278+
else:
279+
self.log_cli_handler = None
280+
self.live_logs_context = _dummy_context_manager()
271281

272282
log_file = get_option_ini(config, 'log_file')
273283
if log_file:
@@ -328,7 +338,7 @@ def pytest_runtest_teardown(self, item):
328338
@pytest.hookimpl(hookwrapper=True)
329339
def pytest_runtestloop(self, session):
330340
"""Runs all collected test items."""
331-
with self.live_logs:
341+
with self.live_logs_context:
332342
if self.log_file_handler is not None:
333343
with closing(self.log_file_handler):
334344
with catching_logs(self.log_file_handler,

testing/logging/test_reporting.py

+29
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,26 @@ def test_foo():
141141
result.stdout.fnmatch_lines(['*- Captured *log call -*'])
142142

143143

144+
@pytest.mark.parametrize('enabled', [True, False])
145+
def test_log_cli_enabled_disabled(testdir, enabled):
146+
msg = 'critical message logged by test'
147+
testdir.makepyfile('''
148+
import logging
149+
def test_log_cli():
150+
logging.critical("{}")
151+
'''.format(msg))
152+
if enabled:
153+
testdir.makeini('''
154+
[pytest]
155+
log_cli=true
156+
''')
157+
result = testdir.runpytest('-s')
158+
if enabled:
159+
assert msg in result.stderr.str()
160+
else:
161+
assert msg not in result.stderr.str()
162+
163+
144164
def test_log_cli_default_level(testdir):
145165
# Default log file level
146166
testdir.makepyfile('''
@@ -153,6 +173,10 @@ def test_log_cli(request):
153173
logging.getLogger('catchlog').warning("This log message will be shown")
154174
print('PASSED')
155175
''')
176+
testdir.makeini('''
177+
[pytest]
178+
log_cli=true
179+
''')
156180

157181
result = testdir.runpytest('-s')
158182

@@ -186,6 +210,10 @@ def test_log_cli(request):
186210
logging.getLogger('catchlog').info("This log message will be shown")
187211
print('PASSED')
188212
''')
213+
testdir.makeini('''
214+
[pytest]
215+
log_cli=true
216+
''')
189217

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

@@ -230,6 +258,7 @@ def test_log_cli_ini_level(testdir):
230258
testdir.makeini(
231259
"""
232260
[pytest]
261+
log_cli=true
233262
log_cli_level = INFO
234263
""")
235264
testdir.makepyfile('''

0 commit comments

Comments
 (0)