Skip to content

Commit 6e3ddf8

Browse files
committed
1 parent 3862b0b commit 6e3ddf8

File tree

1 file changed

+35
-7
lines changed

1 file changed

+35
-7
lines changed

_pytest/logging.py

+35-7
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ def add_option_ini(option, dest, default=None, type=None, **kwargs):
7676
'--log-file-date-format',
7777
dest='log_file_date_format', default=DEFAULT_LOG_DATE_FORMAT,
7878
help='log date format as used by the logging module.')
79+
add_option_ini(
80+
'--capture-log', choices=['on-failure', 'off', 'live'],
81+
dest='capture_log', default='on-failure')
7982

8083

8184
@contextmanager
@@ -117,6 +120,16 @@ def emit(self, record):
117120
logging.StreamHandler.emit(self, record)
118121

119122

123+
class TerminalWriterHandler(logging.Handler):
124+
def __init__(self):
125+
# TODO sys.stderr is captured by default ?!??!
126+
self.tw = py.io.TerminalWriter(sys.stderr)
127+
super(TerminalWriterHandler, self).__init__()
128+
129+
def emit(self, record):
130+
self.tw.write(self.format(record))
131+
132+
120133
class LogCaptureFixture(object):
121134
"""Provides access and control of log capturing."""
122135

@@ -241,6 +254,8 @@ def __init__(self, config):
241254
The formatter can be safely shared across all handlers so
242255
create a single one for the entire test session here.
243256
"""
257+
self.capture_log = get_option_ini(config, 'capture_log')
258+
244259
self.log_cli_level = get_actual_log_level(
245260
config, 'log_cli_level', 'log_level') or logging.WARNING
246261

@@ -249,18 +264,14 @@ def __init__(self, config):
249264
get_option_ini(config, 'log_format'),
250265
get_option_ini(config, 'log_date_format'))
251266

252-
log_cli_handler = logging.StreamHandler(sys.stderr)
267+
253268
log_cli_format = get_option_ini(
254269
config, 'log_cli_format', 'log_format')
255270
log_cli_date_format = get_option_ini(
256271
config, 'log_cli_date_format', 'log_date_format')
257-
log_cli_formatter = logging.Formatter(
272+
self.log_cli_formatter = logging.Formatter(
258273
log_cli_format,
259274
datefmt=log_cli_date_format)
260-
self.log_cli_handler = log_cli_handler # needed for a single unittest
261-
self.live_logs = catching_logs(log_cli_handler,
262-
formatter=log_cli_formatter,
263-
level=self.log_cli_level)
264275

265276
log_file = get_option_ini(config, 'log_file')
266277
if log_file:
@@ -316,11 +327,28 @@ def pytest_runtest_teardown(self, item):
316327
@pytest.hookimpl(hookwrapper=True)
317328
def pytest_runtestloop(self, session):
318329
"""Runs all collected test items."""
319-
with self.live_logs:
330+
331+
# TODO what should happen at the end of the tests?
332+
if self.capture_log == 'live':
333+
with catching_logs(TerminalWriterHandler(),
334+
formatter=self.log_cli_formatter,
335+
level=self.log_cli_level):
336+
if self.log_file_handler is not None:
337+
with closing(self.log_file_handler):
338+
with catching_logs(self.log_file_handler,
339+
level=self.log_file_level):
340+
yield # run all the tests
341+
else:
342+
yield # run all the tests
343+
elif self.capture_log == 'on-failure':
320344
if self.log_file_handler is not None:
321345
with closing(self.log_file_handler):
322346
with catching_logs(self.log_file_handler,
323347
level=self.log_file_level):
324348
yield # run all the tests
325349
else:
326350
yield # run all the tests
351+
elif self.capture_log == 'off':
352+
yield
353+
else:
354+
raise ValueError('capture_log: %s' % capture_log)

0 commit comments

Comments
 (0)