logging: optimize catching_logs slightly
Remove usage of `@contextmanager` as it is a bit slower than hand-rolling, and also disallows re-entry which we want to use. Removing protections around addHandler()/removeHandler(), because logging already checks that internally.
This commit is contained in:
parent
eceb28e4be
commit
e48ac692de
|
@ -272,30 +272,31 @@ def pytest_addoption(parser):
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@contextmanager
|
# Not using @contextmanager for performance reasons.
|
||||||
def catching_logs(handler, level=None):
|
class catching_logs:
|
||||||
"""Context manager that prepares the whole logging machinery properly."""
|
"""Context manager that prepares the whole logging machinery properly."""
|
||||||
root_logger = logging.getLogger()
|
|
||||||
|
|
||||||
if level is not None:
|
__slots__ = ("handler", "level", "orig_level")
|
||||||
handler.setLevel(level)
|
|
||||||
|
|
||||||
# Adding the same handler twice would confuse logging system.
|
def __init__(self, handler, level=None):
|
||||||
# Just don't do that.
|
self.handler = handler
|
||||||
add_new_handler = handler not in root_logger.handlers
|
self.level = level
|
||||||
|
|
||||||
if add_new_handler:
|
def __enter__(self):
|
||||||
root_logger.addHandler(handler)
|
root_logger = logging.getLogger()
|
||||||
if level is not None:
|
if self.level is not None:
|
||||||
orig_level = root_logger.level
|
self.handler.setLevel(self.level)
|
||||||
root_logger.setLevel(min(orig_level, level))
|
root_logger.addHandler(self.handler)
|
||||||
try:
|
if self.level is not None:
|
||||||
yield handler
|
self.orig_level = root_logger.level
|
||||||
finally:
|
root_logger.setLevel(min(self.orig_level, self.level))
|
||||||
if level is not None:
|
return self.handler
|
||||||
root_logger.setLevel(orig_level)
|
|
||||||
if add_new_handler:
|
def __exit__(self, type, value, traceback):
|
||||||
root_logger.removeHandler(handler)
|
root_logger = logging.getLogger()
|
||||||
|
if self.level is not None:
|
||||||
|
root_logger.setLevel(self.orig_level)
|
||||||
|
root_logger.removeHandler(self.handler)
|
||||||
|
|
||||||
|
|
||||||
class LogCaptureHandler(logging.StreamHandler):
|
class LogCaptureHandler(logging.StreamHandler):
|
||||||
|
@ -527,15 +528,11 @@ class LoggingPlugin:
|
||||||
else:
|
else:
|
||||||
self.log_file_handler = None
|
self.log_file_handler = None
|
||||||
|
|
||||||
self.log_cli_handler = None
|
|
||||||
|
|
||||||
self.live_logs_context = lambda: nullcontext()
|
|
||||||
# Note that the lambda for the live_logs_context is needed because
|
|
||||||
# live_logs_context can otherwise not be entered multiple times due
|
|
||||||
# to limitations of contextlib.contextmanager.
|
|
||||||
|
|
||||||
if self._log_cli_enabled():
|
if self._log_cli_enabled():
|
||||||
self._setup_cli_logging()
|
self._setup_cli_logging()
|
||||||
|
else:
|
||||||
|
self.log_cli_handler = None
|
||||||
|
self.live_logs_context = nullcontext
|
||||||
|
|
||||||
def _create_formatter(self, log_format, log_date_format, auto_indent):
|
def _create_formatter(self, log_format, log_date_format, auto_indent):
|
||||||
# color option doesn't exist if terminal plugin is disabled
|
# color option doesn't exist if terminal plugin is disabled
|
||||||
|
|
Loading…
Reference in New Issue