-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨⚡🔨🚨feat(settings): Add SettingsManager class to manage Django loggin…
…g configurations - Added SettingsManager class to handle and organize all DJANGO_LOGGING configurations. - The class initializes all logging configurations at once and provides easy access through attributes. - Default values are used if specific settings are not defined in Django settings. - Refactored get_config method: - Updated to retrieve logging configurations from the SettingsManager. - This refactor improves efficiency and code readability by allowing direct access to individual settings. - Updated checks to fetch logging settings directly from the SettingsManager class. - Refactored related tests: - Adjusted test cases to reflect the new settings access method via SettingsManager. - Ensured that the tests validate the correct functionality of the new settings manager and its configurations. This update improves the overall maintainability and performance of the logging configuration management in the project. Closes #124
- Loading branch information
1 parent
43ef34d
commit 3c9e9a5
Showing
10 changed files
with
229 additions
and
320 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from .manager import settings_manager |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
import os | ||
from typing import Any, Dict, List, Union | ||
|
||
from django.conf import settings | ||
|
||
from django_logging.constants import DefaultConsoleSettings, DefaultLoggingSettings | ||
from django_logging.constants.config_types import ( | ||
ExtraLogFiles, | ||
FormatOption, | ||
LogDateFormat, | ||
LogDir, | ||
LogEmailNotifier, | ||
LogFileFormats, | ||
LogFileFormatTypes, | ||
LogLevels, | ||
NotifierLogLevels, | ||
) | ||
|
||
|
||
# pylint: disable=too-many-instance-attributes | ||
class SettingsManager: | ||
"""Manages DJANGO_LOGGING settings for the Django Logging application. All | ||
configurations are initialized at once and accessible via attributes. | ||
Attributes: | ||
log_dir (str): The directory where log files are stored. | ||
log_levels (List[str]): List of logging levels (e.g., DEBUG, INFO). | ||
log_file_formats (Dict[str, Union[int, str]]): Log file formats, which can be integers or strings. | ||
log_file_format_types (Dict[str, str]): Format types (e.g., 'json', 'xml', 'flat', 'normal') for each logging level. | ||
extra_log_files (Dict[str, bool]): Boolean values indicating whether separate files (JSON, XML) should be used for each logging level. | ||
console_level (str): The logging level for console output. | ||
console_format (Union[int, str]): The format for console logs, either an integer or a string. | ||
colorize_console (bool): Whether to colorize console logs. | ||
log_date_format (str): The format used for timestamps in logs. | ||
email_notifier (Dict[str, Any]): Configuration for email notifications. | ||
email_notifier_enabled (bool): Whether email notifications are enabled. | ||
email_notifier_log_levels (List[str]): Levels that trigger email notifications. | ||
email_notifier_log_format (Union[int, str]): Log format used in email notifications, either an integer or a format string. | ||
auto_initialization_enabled (bool): Whether auto initialization of logging is enabled. | ||
initialization_message_enabled (bool): Whether to display an initialization message for logging. | ||
log_sql_queries_enabled (bool): Whether to log SQL queries in each request. | ||
log_dir_size_limit (int): The maximum size (in MB) allowed for the log directory. | ||
include_log_iboard (bool): Whether the Logiboard feature is included. | ||
use_email_notifier_template (bool): Whether to use a template for email notifications. | ||
""" | ||
|
||
def __init__(self) -> None: | ||
"""Initializes all settings from the Django settings, falling back to | ||
default values defined in DefaultLoggingSettings and | ||
DefaultConsoleSettings.""" | ||
self.log_settings = getattr(settings, "DJANGO_LOGGING", {}) | ||
if not isinstance(self.log_settings, dict): | ||
raise ValueError("DJANGO_LOGGING must be a dictionary with configs as keys") | ||
|
||
self.default_logging = DefaultLoggingSettings() | ||
self.default_console = DefaultConsoleSettings() | ||
|
||
# Initialize all configuration settings | ||
self.log_dir: LogDir = self.get( | ||
"LOG_DIR", os.path.join(os.getcwd(), self.default_logging.log_dir) | ||
) | ||
self.log_levels: LogLevels = self.get( | ||
"LOG_FILE_LEVELS", self.default_logging.log_levels | ||
) | ||
self.log_file_formats: LogFileFormats = self.get( | ||
"LOG_FILE_FORMATS", self.default_logging.log_file_formats | ||
) | ||
self.log_file_format_types: LogFileFormatTypes = self.get( | ||
"LOG_FILE_FORMAT_TYPES", self.default_logging.log_file_format_types | ||
) | ||
self.extra_log_files: ExtraLogFiles = self.get( | ||
"EXTRA_LOG_FILES", self.default_logging.extra_log_files | ||
) | ||
self.console_level: str = self.get( | ||
"LOG_CONSOLE_LEVEL", self.default_console.log_console_level | ||
) | ||
self.console_format: FormatOption = self.get( | ||
"LOG_CONSOLE_FORMAT", self.default_console.log_console_format | ||
) | ||
self.colorize_console: bool = self.get( | ||
"LOG_CONSOLE_COLORIZE", self.default_console.log_console_colorize | ||
) | ||
self.log_date_format: LogDateFormat = self.get( | ||
"LOG_DATE_FORMAT", self.default_logging.log_date_format | ||
) | ||
self.email_notifier: LogEmailNotifier = self.get( | ||
"LOG_EMAIL_NOTIFIER", self.default_logging.log_email_notifier | ||
) | ||
self.email_notifier_enabled: bool = self.email_notifier.get("ENABLE", False) | ||
self.email_notifier_log_levels: NotifierLogLevels = [ | ||
"ERROR" if self.email_notifier.get("NOTIFY_ERROR", False) else None, | ||
("CRITICAL" if self.email_notifier.get("NOTIFY_CRITICAL", False) else None), | ||
] | ||
self.email_notifier_log_format: FormatOption = self.email_notifier.get( | ||
"LOG_FORMAT", 1 | ||
) | ||
self.auto_initialization_enabled: bool = self.get( | ||
"AUTO_INITIALIZATION_ENABLE", | ||
self.default_logging.auto_initialization_enable, | ||
) | ||
self.initialization_message_enabled: bool = self.get( | ||
"INITIALIZATION_MESSAGE_ENABLE", | ||
self.default_logging.initialization_message_enable, | ||
) | ||
self.log_sql_queries_enabled: bool = self.get( | ||
"LOG_SQL_QUERIES_ENABLE", self.default_logging.log_sql_queries_enable | ||
) | ||
self.log_dir_size_limit: int = self.get( | ||
"LOG_DIR_SIZE_LIMIT", self.default_logging.log_dir_size_limit | ||
) | ||
self.include_log_iboard: bool = self.get( | ||
"INCLUDE_LOG_iBOARD", self.default_logging.include_log_iboard | ||
) | ||
self.use_email_notifier_template: bool = self.email_notifier.get( | ||
"USE_TEMPLATE", True | ||
) | ||
|
||
def get(self, key: str, default_value: Any) -> Any: | ||
"""Retrieves a logging-related setting from the Django settings. If the | ||
setting is not present, returns the provided default value. | ||
Args: | ||
key (str): The key to look up in the logging settings. | ||
default_value (Any): The default value to return if the key is not found. | ||
Returns: | ||
Any: The value of the setting or the default value. | ||
""" | ||
return self.log_settings.get(key, default_value) | ||
|
||
|
||
settings_manager: SettingsManager = SettingsManager() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -73,7 +73,7 @@ def test_command_log_directory_size_exceeds_limit( | |
with patch("os.path.getsize", side_effect=[60 * 1024 * 1024, 50 * 1024 * 1024]): | ||
out = StringIO() | ||
with patch("django.conf.settings.ADMIN_EMAIL", "[email protected]"): | ||
with patch("django.conf.settings.DJANGO_LOGGING", {"LOG_DIR_SIZE_LIMIT": 100}): | ||
with patch("django_logging.management.commands.logs_size_audit.settings_manager.log_dir_size_limit", 100): | ||
call_command("logs_size_audit", stdout=out) | ||
|
||
# Verify that the warning email was sent | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.