Skip to content

Commit 8f6a592

Browse files
committed
Add newline before log messages and enable -v output when log_cli is enabled
1 parent 5d89a93 commit 8f6a592

File tree

3 files changed

+31
-3
lines changed

3 files changed

+31
-3
lines changed

_pytest/logging.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,13 @@ def __init__(self, config):
271271
create a single one for the entire test session here.
272272
"""
273273
self._config = config
274+
275+
# enable verbose output automatically if live logging is enabled
276+
if self._config.getini('log_cli') and not config.getoption('verbose'):
277+
# sanity check: terminal reporter should not have been loaded at this point
278+
assert self._config.pluginmanager.get_plugin('terminalreporter') is None
279+
config.option.verbose = 1
280+
274281
self.print_logs = get_option_ini(config, 'log_print')
275282
self.formatter = logging.Formatter(
276283
get_option_ini(config, 'log_format'),
@@ -352,7 +359,7 @@ def _setup_cli_logging(self):
352359
"""
353360
terminal_reporter = self._config.pluginmanager.get_plugin('terminalreporter')
354361
if self._config.getini('log_cli') and terminal_reporter is not None:
355-
log_cli_handler = logging.StreamHandler(terminal_reporter._tw)
362+
log_cli_handler = _LiveLoggingStreamHandler(terminal_reporter._tw)
356363
log_cli_format = get_option_ini(
357364
self._config, 'log_cli_format', 'log_format')
358365
log_cli_date_format = get_option_ini(
@@ -368,3 +375,18 @@ def _setup_cli_logging(self):
368375
else:
369376
self.log_cli_handler = None
370377
self.live_logs_context = _dummy_context_manager()
378+
379+
380+
class _LiveLoggingStreamHandler(logging.StreamHandler):
381+
"""
382+
Custom StreamHandler used by the live logging feature: it will write a newline before the first log message
383+
in each test.
384+
"""
385+
386+
def emit(self, record):
387+
if not getattr(self, '_first_record_emitted', False):
388+
self.stream.write('\n')
389+
# we might consider adding a header at this point using self.stream.sep('-', 'live log') or something
390+
# similar when we improve live logging output
391+
self._first_record_emitted = True
392+
logging.StreamHandler.emit(self, record)

doc/en/logging.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,8 @@ made in ``3.4`` after community feedback:
228228
* Log levels are no longer changed unless explicitly requested by the :confval:`log_level` configuration
229229
or ``--log-level`` command-line options. This allows users to configure logger objects themselves.
230230
* :ref:`Live Logs <live_logs>` is now disabled by default and can be enabled setting the
231-
:confval:`log_cli` configuration option to ``true``.
231+
:confval:`log_cli` configuration option to ``true``. When enabled, the verbosity is increased so logging for each
232+
test is visible.
232233
* :ref:`Live Logs <live_logs>` are now sent to ``sys.stdout`` and no longer require the ``-s`` command-line option
233234
to work.
234235

testing/logging/test_reporting.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,11 @@ def test_log_cli():
156156
''')
157157
result = testdir.runpytest('-s')
158158
if enabled:
159-
assert msg in result.stdout.str()
159+
result.stdout.fnmatch_lines([
160+
'test_log_cli_enabled_disabled.py::test_log_cli ',
161+
'test_log_cli_enabled_disabled.py* CRITICAL critical message logged by test',
162+
'PASSED',
163+
])
160164
else:
161165
assert msg not in result.stdout.str()
162166

@@ -181,6 +185,7 @@ def test_log_cli(request):
181185

182186
# fnmatch_lines does an assertion internally
183187
result.stdout.fnmatch_lines([
188+
'test_log_cli_default_level.py::test_log_cli ',
184189
'test_log_cli_default_level.py*WARNING message will be shown*',
185190
])
186191
assert "INFO message won't be shown" not in result.stdout.str()

0 commit comments

Comments
 (0)