From dd124c24548d2fdcbacbb697ab9945e2abdd0d95 Mon Sep 17 00:00:00 2001 From: Niklas Beierl <22919961+NiklasBeierl@users.noreply.github.com> Date: Sat, 3 Feb 2024 00:39:04 +0100 Subject: [PATCH] New: Custom log field for correlation ids --- django_guid/log_filters.py | 13 +++++++------ docs/configuration.rst | 5 ++++- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/django_guid/log_filters.py b/django_guid/log_filters.py index d252846..2ca02c4 100644 --- a/django_guid/log_filters.py +++ b/django_guid/log_filters.py @@ -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 diff --git a/docs/configuration.rst b/docs/configuration.rst index b578d7c..3c15bdf 100644 --- a/docs/configuration.rst +++ b/docs/configuration.rst @@ -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' } } }