Skip to content

Commit 935f967

Browse files
clokepphil-flex
authored andcommitted
Add backwards compatibility codepath to LoggingContext. (matrix-org#7408)
1 parent 6e9d9d8 commit 935f967

2 files changed

Lines changed: 42 additions & 0 deletions

File tree

changelog.d/7408.misc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Clean up some LoggingContext code.

synapse/logging/context.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import logging
2828
import threading
2929
import types
30+
import warnings
3031
from typing import TYPE_CHECKING, Optional, Tuple, TypeVar, Union
3132

3233
from typing_extensions import Literal
@@ -287,6 +288,46 @@ def __str__(self) -> str:
287288
return str(self.request)
288289
return "%s@%x" % (self.name, id(self))
289290

291+
@classmethod
292+
def current_context(cls) -> LoggingContextOrSentinel:
293+
"""Get the current logging context from thread local storage
294+
295+
This exists for backwards compatibility. ``current_context()`` should be
296+
called directly.
297+
298+
Returns:
299+
LoggingContext: the current logging context
300+
"""
301+
warnings.warn(
302+
"synapse.logging.context.LoggingContext.current_context() is deprecated "
303+
"in favor of synapse.logging.context.current_context().",
304+
DeprecationWarning,
305+
stacklevel=2,
306+
)
307+
return current_context()
308+
309+
@classmethod
310+
def set_current_context(
311+
cls, context: LoggingContextOrSentinel
312+
) -> LoggingContextOrSentinel:
313+
"""Set the current logging context in thread local storage
314+
315+
This exists for backwards compatibility. ``set_current_context()`` should be
316+
called directly.
317+
318+
Args:
319+
context(LoggingContext): The context to activate.
320+
Returns:
321+
The context that was previously active
322+
"""
323+
warnings.warn(
324+
"synapse.logging.context.LoggingContext.set_current_context() is deprecated "
325+
"in favor of synapse.logging.context.set_current_context().",
326+
DeprecationWarning,
327+
stacklevel=2,
328+
)
329+
return set_current_context(context)
330+
290331
def __enter__(self) -> "LoggingContext":
291332
"""Enters this logging context into thread local storage"""
292333
old_context = set_current_context(self)

0 commit comments

Comments
 (0)