Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Commit

Permalink
Make LoggingContext's name optional
Browse files Browse the repository at this point in the history
  • Loading branch information
richvdh committed Apr 21, 2021
1 parent bdb4c20 commit c4ee004
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
1 change: 1 addition & 0 deletions changelog.d/9857.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix a regression in Synapse v1.32.1 which caused `LoggingContext` errors in plugins.
15 changes: 12 additions & 3 deletions synapse/logging/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
"""

Expand All @@ -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()
Expand Down Expand Up @@ -314,6 +314,15 @@ def __init__(
# 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 not name:
name = str(self.parent_context)
if not name:
raise ValueError(
"LoggingContext must be given either a name or a parent context"
)
self.name = name

def __str__(self) -> str:
return self.name

Expand Down

0 comments on commit c4ee004

Please sign in to comment.