summary refs log tree commit diff
diff options
context:
space:
mode:
authorRichard van der Hoff <1389908+richvdh@users.noreply.github.com>2021-04-21 16:39:34 +0100
committerGitHub <noreply@github.com>2021-04-21 16:39:34 +0100
commitd9bd62f9d1a6238b3f485caee07f9fd399b27134 (patch)
treede983eb50b84379841d058c8c602cee8bdf66142
parentClarify 1.32.0/1 changelog and upgrade notes (diff)
downloadsynapse-d9bd62f9d1a6238b3f485caee07f9fd399b27134.tar.xz
Make LoggingContext's name optional (#9857)
Fixes https://github.com/matrix-org/synapse-s3-storage-provider/issues/55
-rw-r--r--changelog.d/9857.bugfix1
-rw-r--r--synapse/logging/context.py15
2 files changed, 13 insertions, 3 deletions
diff --git a/changelog.d/9857.bugfix b/changelog.d/9857.bugfix
new file mode 100644
index 0000000000..7eed41594d
--- /dev/null
+++ b/changelog.d/9857.bugfix
@@ -0,0 +1 @@
+Fix a regression in Synapse v1.32.1 which caused `LoggingContext` errors in plugins.
diff --git a/synapse/logging/context.py b/synapse/logging/context.py
index dbd7d3a33a..7fc11a9ac2 100644
--- a/synapse/logging/context.py
+++ b/synapse/logging/context.py
@@ -258,7 +258,8 @@ class LoggingContext:
           child to the parent
 
     Args:
-        name (str): Name for the context for debugging.
+        name: Name for the context for logging. If this is omitted, it is
+           inherited from the parent context.
         parent_context (LoggingContext|None): The parent of the new context
     """
 
@@ -277,12 +278,11 @@ class LoggingContext:
 
     def __init__(
         self,
-        name: str,
+        name: Optional[str] = None,
         parent_context: "Optional[LoggingContext]" = None,
         request: Optional[ContextRequest] = None,
     ) -> None:
         self.previous_context = current_context()
-        self.name = name
 
         # track the resources used by this context so far
         self._resource_usage = ContextResourceUsage()
@@ -314,6 +314,15 @@ class LoggingContext:
             # the request param overrides the request from the parent context
             self.request = request
 
+        # if we don't have a `name`, but do have a parent context, use its name.
+        if self.parent_context and name is None:
+            name = str(self.parent_context)
+        if name is None:
+            raise ValueError(
+                "LoggingContext must be given either a name or a parent context"
+            )
+        self.name = name
+
     def __str__(self) -> str:
         return self.name