From 5ad48e2e3a19460c1f7e86770c69a93ed18dafde Mon Sep 17 00:00:00 2001 From: Patrick Cloke Date: Mon, 4 May 2020 10:37:56 -0400 Subject: [PATCH 1/6] Add backwards compatibility codepath to LoggingContext. --- synapse/logging/context.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/synapse/logging/context.py b/synapse/logging/context.py index a8f674d13da9..d4fd34bb60a6 100644 --- a/synapse/logging/context.py +++ b/synapse/logging/context.py @@ -287,6 +287,18 @@ def __str__(self) -> str: return str(self.request) return "%s@%x" % (self.name, id(self)) + @classmethod + def current_context(cls) -> LoggingContextOrSentinel: + """Get the current logging context from thread local storage + + This exists for backwards compatibility. ``current_context()`` should be + called directly. + + Returns: + LoggingContext: the current logging context + """ + return current_context() + def __enter__(self) -> "LoggingContext": """Enters this logging context into thread local storage""" old_context = set_current_context(self) From 6291f8cd7f50302a0d108b6234984629aca0a57e Mon Sep 17 00:00:00 2001 From: Patrick Cloke Date: Mon, 4 May 2020 10:44:00 -0400 Subject: [PATCH 2/6] Add a changelog entry. --- changelog.d/7408.misc | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog.d/7408.misc diff --git a/changelog.d/7408.misc b/changelog.d/7408.misc new file mode 100644 index 000000000000..731f4dcb52e4 --- /dev/null +++ b/changelog.d/7408.misc @@ -0,0 +1 @@ +Clean up some LoggingContext code. From 80aef2146978211634aea79bc5e2132ce796e4b6 Mon Sep 17 00:00:00 2001 From: Patrick Cloke Date: Mon, 4 May 2020 12:35:55 -0400 Subject: [PATCH 3/6] Also add back th set_current_context method. --- synapse/logging/context.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/synapse/logging/context.py b/synapse/logging/context.py index d4fd34bb60a6..8f79b56bc032 100644 --- a/synapse/logging/context.py +++ b/synapse/logging/context.py @@ -299,6 +299,22 @@ def current_context(cls) -> LoggingContextOrSentinel: """ return current_context() + @classmethod + def set_current_context( + cls, context: LoggingContextOrSentinel + ) -> LoggingContextOrSentinel: + """Set the current logging context in thread local storage + + This exists for backwards compatibility. ``set_current_context()`` should be + called directly. + + Args: + context(LoggingContext): The context to activate. + Returns: + The context that was previously active + """ + return set_current_context(context) + def __enter__(self) -> "LoggingContext": """Enters this logging context into thread local storage""" old_context = set_current_context(self) From 0c20d71c9db4355e770f9ca26a5eda9af0d3a12b Mon Sep 17 00:00:00 2001 From: Patrick Cloke Date: Mon, 4 May 2020 13:06:03 -0400 Subject: [PATCH 4/6] Add deprecation warnings. --- synapse/logging/context.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/synapse/logging/context.py b/synapse/logging/context.py index 8f79b56bc032..8a7a555e6937 100644 --- a/synapse/logging/context.py +++ b/synapse/logging/context.py @@ -28,6 +28,7 @@ import threading import types from typing import TYPE_CHECKING, Optional, Tuple, TypeVar, Union +import warnings from typing_extensions import Literal @@ -297,6 +298,11 @@ def current_context(cls) -> LoggingContextOrSentinel: Returns: LoggingContext: the current logging context """ + warnings.warn( + 'synapse.logging.context.LoggingContext.current_context() is deprecated ' + 'in favor of synapse.logging.context.current_context().', + PendingDeprecationWarning, stacklevel=2, + ) return current_context() @classmethod @@ -313,6 +319,11 @@ def set_current_context( Returns: The context that was previously active """ + warnings.warn( + 'synapse.logging.context.LoggingContext.set_current_context() is deprecated ' + 'in favor of synapse.logging.context.set_current_context().', + PendingDeprecationWarning, stacklevel=2, + ) return set_current_context(context) def __enter__(self) -> "LoggingContext": From bd9daecc160d12834a0c1c06c46117f0ff585728 Mon Sep 17 00:00:00 2001 From: Patrick Cloke Date: Mon, 4 May 2020 13:08:32 -0400 Subject: [PATCH 5/6] Lint. --- synapse/logging/context.py | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/synapse/logging/context.py b/synapse/logging/context.py index 8a7a555e6937..d65769370266 100644 --- a/synapse/logging/context.py +++ b/synapse/logging/context.py @@ -27,8 +27,8 @@ import logging import threading import types -from typing import TYPE_CHECKING, Optional, Tuple, TypeVar, Union import warnings +from typing import TYPE_CHECKING, Optional, Tuple, TypeVar, Union from typing_extensions import Literal @@ -299,9 +299,10 @@ def current_context(cls) -> LoggingContextOrSentinel: LoggingContext: the current logging context """ warnings.warn( - 'synapse.logging.context.LoggingContext.current_context() is deprecated ' - 'in favor of synapse.logging.context.current_context().', - PendingDeprecationWarning, stacklevel=2, + "synapse.logging.context.LoggingContext.current_context() is deprecated " + "in favor of synapse.logging.context.current_context().", + PendingDeprecationWarning, + stacklevel=2, ) return current_context() @@ -320,9 +321,10 @@ def set_current_context( The context that was previously active """ warnings.warn( - 'synapse.logging.context.LoggingContext.set_current_context() is deprecated ' - 'in favor of synapse.logging.context.set_current_context().', - PendingDeprecationWarning, stacklevel=2, + "synapse.logging.context.LoggingContext.set_current_context() is deprecated " + "in favor of synapse.logging.context.set_current_context().", + PendingDeprecationWarning, + stacklevel=2, ) return set_current_context(context) From c152ad937d49c203c14009995a3ec2df3a799759 Mon Sep 17 00:00:00 2001 From: Patrick Cloke Date: Tue, 5 May 2020 07:37:30 -0400 Subject: [PATCH 6/6] Switch to DeprecationWarning. --- synapse/logging/context.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/synapse/logging/context.py b/synapse/logging/context.py index d65769370266..856534e91a75 100644 --- a/synapse/logging/context.py +++ b/synapse/logging/context.py @@ -301,7 +301,7 @@ def current_context(cls) -> LoggingContextOrSentinel: warnings.warn( "synapse.logging.context.LoggingContext.current_context() is deprecated " "in favor of synapse.logging.context.current_context().", - PendingDeprecationWarning, + DeprecationWarning, stacklevel=2, ) return current_context() @@ -323,7 +323,7 @@ def set_current_context( warnings.warn( "synapse.logging.context.LoggingContext.set_current_context() is deprecated " "in favor of synapse.logging.context.set_current_context().", - PendingDeprecationWarning, + DeprecationWarning, stacklevel=2, ) return set_current_context(context)