From 9fdf063b88b40043ed19ff7474ccd629ea297466 Mon Sep 17 00:00:00 2001 From: MEHRSHAD MIRSHEKARY Date: Sat, 17 Aug 2024 16:57:18 +0330 Subject: [PATCH] :bug::zap::bulb: fix: filtering log records in log file handlers Added Introduced LoggingLevelFilter class to filter log records based on their logging level. Includes comprehensive docstrings for Filter Class that explains the filtering logic. Ensures that log records are written only to their respective log files, preventing higher-level logs from appearing in lower-level files. Updated Modified the logging configuration in conf to apply the LoggingLevelFilter to each file handler. Updated the handlers dictionary to include the newly created filter for each log level. Introduced a filters dictionary in the logging configuration to manage the filters for each log level. Ensured that each log level's handler is correctly associated with its corresponding filter. Closes #4 --- django_logging/filters/__init__.py | 0 django_logging/filters/level_filter.py | 38 ++++++++++++++++++++++++++ django_logging/settings/conf.py | 12 ++++++++ 3 files changed, 50 insertions(+) create mode 100644 django_logging/filters/__init__.py create mode 100644 django_logging/filters/level_filter.py diff --git a/django_logging/filters/__init__.py b/django_logging/filters/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/django_logging/filters/level_filter.py b/django_logging/filters/level_filter.py new file mode 100644 index 0000000..b147322 --- /dev/null +++ b/django_logging/filters/level_filter.py @@ -0,0 +1,38 @@ +import logging + + +class LoggingLevelFilter(logging.Filter): + """ + Filters log records based on their logging level. + + This filter is used to prevent log records from being written to log files + intended for lower log levels. For example, if we have separate log + files for DEBUG, INFO, WARNING, and ERROR levels, this filter ensures that + a log record with level ERROR is only written to the ERROR log file, and not + to the DEBUG, INFO or WARNING log files. + """ + + def __init__(self, logging_level: int): + """ + Initializes a LoggingLevelFilter instance. + + Args: + logging_level: The logging level to filter on (e.g. logging.DEBUG, logging.INFO, etc.). + + Returns: + None + """ + super().__init__() + self.logging_level = logging_level + + def filter(self, record: logging.LogRecord) -> bool: + """ + Filters a log record based on its level. + + Args: + record: The log record to filter. + + Returns: + True if the log record's level matches the specified logging level, False otherwise. + """ + return record.levelno == self.logging_level diff --git a/django_logging/settings/conf.py b/django_logging/settings/conf.py index dba4aff..60ff671 100644 --- a/django_logging/settings/conf.py +++ b/django_logging/settings/conf.py @@ -3,6 +3,8 @@ import os from typing import List, Dict, Optional +from django_logging.filters.level_filter import LoggingLevelFilter + class LogConfig: """ @@ -67,6 +69,7 @@ def set_conf(self) -> None: "filename": log_file, "formatter": "default", "level": level, + "filters": [level.lower()] } for level, log_file in self.log_files.items() } @@ -76,6 +79,14 @@ def set_conf(self) -> None: "level": "DEBUG", } + filters = { + level.lower(): { + "()": LoggingLevelFilter, + "logging_level": getattr(logging, level), + } + for level in self.log_config.log_levels + } + loggers = { level.lower(): { "level": level, @@ -88,6 +99,7 @@ def set_conf(self) -> None: config = { "version": 1, "handlers": handlers, + "filters": filters, "loggers": loggers, "root": {"level": "DEBUG", "handlers": list(handlers.keys())}, "disable_existing_loggers": False,