Skip to content
Snippets Groups Projects
Unverified Commit 15997618 authored by Richard van der Hoff's avatar Richard van der Hoff Committed by GitHub
Browse files

Clean up PreserveLoggingContext (#7877)

This had some dead code and some just plain wrong docstrings.
parent 2ccd48e9
No related branches found
No related tags found
No related merge requests found
Clean up `PreserveLoggingContext`.
......@@ -566,36 +566,33 @@ class LoggingContextFilter(logging.Filter):
return True
class PreserveLoggingContext(object):
"""Captures the current logging context and restores it when the scope is
exited. Used to restore the context after a function using
@defer.inlineCallbacks is resumed by a callback from the reactor."""
class PreserveLoggingContext:
"""Context manager which replaces the logging context
__slots__ = ["current_context", "new_context", "has_parent"]
The previous logging context is restored on exit."""
__slots__ = ["_old_context", "_new_context"]
def __init__(
self, new_context: LoggingContextOrSentinel = SENTINEL_CONTEXT
) -> None:
self.new_context = new_context
self._new_context = new_context
def __enter__(self) -> None:
"""Captures the current logging 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
self._old_context = set_current_context(self._new_context)
def __exit__(self, type, value, traceback) -> None:
"""Restores the current logging context"""
context = set_current_context(self.current_context)
context = set_current_context(self._old_context)
if context != self.new_context:
if context != self._new_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:
logger.warning(
"Expected logging context %s but found %s",
self.new_context,
self._new_context,
context,
)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment