|
27 | 27 | import logging |
28 | 28 | import threading |
29 | 29 | import types |
| 30 | +import warnings |
30 | 31 | from typing import TYPE_CHECKING, Optional, Tuple, TypeVar, Union |
31 | 32 |
|
32 | 33 | from typing_extensions import Literal |
@@ -287,6 +288,46 @@ def __str__(self) -> str: |
287 | 288 | return str(self.request) |
288 | 289 | return "%s@%x" % (self.name, id(self)) |
289 | 290 |
|
| 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 | + |
290 | 331 | def __enter__(self) -> "LoggingContext": |
291 | 332 | """Enters this logging context into thread local storage""" |
292 | 333 | old_context = set_current_context(self) |
|
0 commit comments