Integrate logging_using_handler into catching_logs

logging_using_handler is only used in catching_logs. Therefore it makes
sense to remove one of the many context managers from the logging
plugin.
This commit is contained in:
Thomas Hisch 2017-12-07 15:35:23 +01:00
parent 88f2cc9b64
commit ba209b5230
1 changed files with 14 additions and 24 deletions

View File

@ -78,23 +78,6 @@ def pytest_addoption(parser):
help='log date format as used by the logging module.')
@contextmanager
def logging_using_handler(handler, logger=None):
"""Context manager that safely registers a given handler."""
logger = logger or logging.getLogger(logger)
if handler in logger.handlers: # reentrancy
# Adding the same handler twice would confuse logging system.
# Just don't do that.
yield
else:
logger.addHandler(handler)
try:
yield
finally:
logger.removeHandler(handler)
@contextmanager
def catching_logs(handler, formatter=None,
level=logging.NOTSET, logger=None):
@ -105,13 +88,20 @@ def catching_logs(handler, formatter=None,
handler.setFormatter(formatter)
handler.setLevel(level)
with logging_using_handler(handler, logger):
orig_level = logger.level
logger.setLevel(min(orig_level, level))
try:
yield handler
finally:
logger.setLevel(orig_level)
# Adding the same handler twice would confuse logging system.
# Just don't do that.
add_new_handler = handler not in logger.handlers
if add_new_handler:
logger.addHandler(handler)
orig_level = logger.level
logger.setLevel(min(orig_level, level))
try:
yield handler
finally:
logger.setLevel(orig_level)
if add_new_handler:
logger.removeHandler(handler)
class LogCaptureHandler(logging.StreamHandler):