Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix CorrelationIdFilter for loguru #102

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions asgi_correlation_id/log_filters.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from logging import Filter
from typing import TYPE_CHECKING, Optional
from typing import TYPE_CHECKING, Any, Dict, Optional, Union

from asgi_correlation_id.context import celery_current_id, celery_parent_id, correlation_id

Expand All @@ -22,7 +22,7 @@ def __init__(self, name: str = '', uuid_length: Optional[int] = None, default_va
self.uuid_length = uuid_length
self.default_value = default_value

def filter(self, record: 'LogRecord') -> bool:
def filter(self, record: Union['LogRecord', Dict[str, Any]]) -> bool:
"""
Attach a correlation ID to the log record.

Expand All @@ -32,7 +32,11 @@ def filter(self, record: 'LogRecord') -> bool:
metadata.
"""
cid = correlation_id.get(self.default_value)
record.correlation_id = _trim_string(cid, self.uuid_length)
formatted_cid = _trim_string(cid, self.uuid_length)
if isinstance(record, dict):
record['correlation_id'] = formatted_cid
else:
record.correlation_id = formatted_cid
return True


Expand Down
Loading
Loading