Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
twmr committed Dec 13, 2017
1 parent 3862b0b commit 6e3ddf8
Showing 1 changed file with 35 additions and 7 deletions.
42 changes: 35 additions & 7 deletions _pytest/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ def add_option_ini(option, dest, default=None, type=None, **kwargs):
'--log-file-date-format',
dest='log_file_date_format', default=DEFAULT_LOG_DATE_FORMAT,
help='log date format as used by the logging module.')
add_option_ini(
'--capture-log', choices=['on-failure', 'off', 'live'],
dest='capture_log', default='on-failure')


@contextmanager
Expand Down Expand Up @@ -117,6 +120,16 @@ def emit(self, record):
logging.StreamHandler.emit(self, record)


class TerminalWriterHandler(logging.Handler):
def __init__(self):
# TODO sys.stderr is captured by default ?!??!
self.tw = py.io.TerminalWriter(sys.stderr)
super(TerminalWriterHandler, self).__init__()

def emit(self, record):
self.tw.write(self.format(record))


class LogCaptureFixture(object):
"""Provides access and control of log capturing."""

Expand Down Expand Up @@ -241,6 +254,8 @@ 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.capture_log = get_option_ini(config, 'capture_log')

self.log_cli_level = get_actual_log_level(
config, 'log_cli_level', 'log_level') or logging.WARNING

Expand All @@ -249,18 +264,14 @@ def __init__(self, config):
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(
self.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)

log_file = get_option_ini(config, 'log_file')
if log_file:
Expand Down Expand Up @@ -316,11 +327,28 @@ def pytest_runtest_teardown(self, item):
@pytest.hookimpl(hookwrapper=True)
def pytest_runtestloop(self, session):
"""Runs all collected test items."""
with self.live_logs:

# TODO what should happen at the end of the tests?
if self.capture_log == 'live':
with catching_logs(TerminalWriterHandler(),
formatter=self.log_cli_formatter,
level=self.log_cli_level):
if self.log_file_handler is not None:
with closing(self.log_file_handler):
with catching_logs(self.log_file_handler,
level=self.log_file_level):
yield # run all the tests
else:
yield # run all the tests
elif self.capture_log == 'on-failure':
if self.log_file_handler is not None:
with closing(self.log_file_handler):
with catching_logs(self.log_file_handler,
level=self.log_file_level):
yield # run all the tests
else:
yield # run all the tests
elif self.capture_log == 'off':
yield
else:
raise ValueError('capture_log: %s' % capture_log)

0 comments on commit 6e3ddf8

Please sign in to comment.