File: log_filters.py

package info (click to toggle)
python-django-guid 3.5.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 664 kB
  • sloc: python: 1,267; makefile: 16
file content (23 lines) | stat: -rw-r--r-- 694 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
from logging import Filter
from typing import TYPE_CHECKING

from django_guid.context import guid

if TYPE_CHECKING:
    from logging import LogRecord


class CorrelationId(Filter):
    def __init__(self, correlation_id_field: str = 'correlation_id') -> None:
        super().__init__()
        self.correlation_id_field = correlation_id_field

    def filter(self, record: 'LogRecord') -> bool:
        """
        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
        """
        setattr(record, self.correlation_id_field, guid.get())
        return True