Clean up PreserveLoggingContext (#7877)

This had some dead code and some just plain wrong docstrings.
This commit is contained in:
Richard van der Hoff 2020-07-22 00:40:27 +01:00 committed by GitHub
parent 2ccd48e921
commit 15997618e2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 16 deletions

1
changelog.d/7877.misc Normal file
View file

@ -0,0 +1 @@
Clean up `PreserveLoggingContext`.

View file

@ -566,36 +566,33 @@ class LoggingContextFilter(logging.Filter):
return True return True
class PreserveLoggingContext(object): class PreserveLoggingContext:
"""Captures the current logging context and restores it when the scope is """Context manager which replaces the logging context
exited. Used to restore the context after a function using
@defer.inlineCallbacks is resumed by a callback from the reactor."""
__slots__ = ["current_context", "new_context", "has_parent"] The previous logging context is restored on exit."""
__slots__ = ["_old_context", "_new_context"]
def __init__( def __init__(
self, new_context: LoggingContextOrSentinel = SENTINEL_CONTEXT self, new_context: LoggingContextOrSentinel = SENTINEL_CONTEXT
) -> None: ) -> None:
self.new_context = new_context self._new_context = new_context
def __enter__(self) -> None: def __enter__(self) -> None:
"""Captures the current logging context""" self._old_context = set_current_context(self._new_context)
self.current_context = set_current_context(self.new_context)
if self.current_context:
self.has_parent = self.current_context.previous_context is not None
def __exit__(self, type, value, traceback) -> None: def __exit__(self, type, value, traceback) -> None:
"""Restores the current logging context""" context = set_current_context(self._old_context)
context = set_current_context(self.current_context)
if context != self.new_context: if context != self._new_context:
if not context: if not context:
logger.warning("Expected logging context %s was lost", self.new_context) logger.warning(
"Expected logging context %s was lost", self._new_context
)
else: else:
logger.warning( logger.warning(
"Expected logging context %s but found %s", "Expected logging context %s but found %s",
self.new_context, self._new_context,
context, context,
) )