Skip to content

Commit

Permalink
feat: logger fix
Browse files Browse the repository at this point in the history
  • Loading branch information
dartt0n committed Jul 20, 2024
1 parent d0434c5 commit 2a1f4af
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
1 change: 0 additions & 1 deletion src/service/allocation.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ async def create(self, allocation: proto.CreateAllocation) -> domain.Allocation:
| proto.CreateRoomedAllocation
| proto.CreateClosedAllocation,
):

if not await common.check_participants_exist(
allocation, self._participant_repo
):
Expand Down
28 changes: 20 additions & 8 deletions src/utils/logger/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def __init__(self, context: str):
raw=False,
capture=True,
patchers=[],
extra={"context": context, "correlation_id": None},
extra={"context": context, "correlation_id": ""},
)
logger_format = (
"<green>{time:YYYY-MM-DD HH:mm:ss.SSS}</green>|"
Expand All @@ -49,25 +49,37 @@ def _finish_record(self, name: str) -> str:
@property
def cor_id(self) -> str | None:
cor_id = COR_ID.get()
return cor_id.hex if cor_id else None
return cor_id.hex if cor_id is not None else ""

def trace(self, message, *args, **kwargs):
super().trace(message, *args, correlation_id=self.cor_id, **kwargs)
if self.cor_id:
kwargs["correlation_id"] = self.cor_id
super().trace(message, *args, **kwargs)

def debug(self, message, *args, **kwargs):
super().debug(message, *args, correlation_id=self.cor_id, **kwargs)
if self.cor_id:
kwargs["correlation_id"] = self.cor_id
super().debug(message, *args, **kwargs)

def info(self, message, *args, **kwargs):
super().info(message, *args, correlation_id=self.cor_id, **kwargs)
if self.cor_id:
kwargs["correlation_id"] = self.cor_id
super().info(message, *args, **kwargs)

def warning(self, message, *args, **kwargs):
super().warning(message, *args, correlation_id=self.cor_id, **kwargs)
if self.cor_id:
kwargs["correlation_id"] = self.cor_id
super().warning(message, *args, **kwargs)

def error(self, message, *args, **kwargs):
super().error(message, *args, correlation_id=self.cor_id, **kwargs)
if self.cor_id:
kwargs["correlation_id"] = self.cor_id
super().error(message, *args, **kwargs)

def critical(self, message, *args, **kwargs):
super().critical(message, *args, correlation_id=self.cor_id, **kwargs)
if self.cor_id:
kwargs["correlation_id"] = self.cor_id
super().critical(message, *args, **kwargs)

@contextmanager
def activity(self, name: str, with_traceback: bool = False, capture: bool = False):
Expand Down

0 comments on commit 2a1f4af

Please sign in to comment.