Skip to content

Commit

Permalink
New: Custom log field for correlation ids
Browse files Browse the repository at this point in the history
  • Loading branch information
NiklasBeierl committed Feb 3, 2024
1 parent 516ddba commit 3010417
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
13 changes: 7 additions & 6 deletions django_guid/log_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@


class CorrelationId(Filter):
def __init__(self, correlation_id_field: str = "correlation_id"):
super().__init__()
self.correlation_id_field = correlation_id_field

def filter(self, record: 'LogRecord') -> bool:
"""
Determines that the specified record is to be logged.
From the docs:
Is the specified record to be logged? Returns 0 for no, nonzero for
yes. If deemed appropriate, the record may be modified in-place.
Add the correlation ID to the log record.
:param record: Log record
:param correlation_id_field: record field on which the correlation id is set
:return: True
"""
record.correlation_id = guid.get()
setattr(record, self.correlation_id_field, guid.get())
return True
5 changes: 4 additions & 1 deletion docs/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@ Add :code:`django_guid.log_filters.CorrelationId` as a filter in your ``LOGGING`
...
'filters': {
'correlation_id': {
'()': 'django_guid.log_filters.CorrelationId'
'()': 'django_guid.log_filters.CorrelationId',
# You can optionally override the record field name where the field is
# stored
'correlation_id_field': 'correlation_id'
}
}
}
Expand Down

0 comments on commit 3010417

Please sign in to comment.