From 47ebea4348848e20bf47ac8d3db22aba7eb62453 Mon Sep 17 00:00:00 2001 From: MEHRSHAD MIRSHEKARY Date: Fri, 4 Oct 2024 16:54:26 +0330 Subject: [PATCH] :zap::sparkles: feat(validators): Add validators for log file format types and extra log files configuration - **Added alidate_log_file_format_types:** This validator ensures that the LOG_FILE_FORMAT_TYPES setting is a dictionary where keys represent valid log levels and values are valid format types (e.g., JSON, XML). - Logs errors if the format type or level is invalid. - Provides detailed hints for correcting misconfigurations. - **Added alidate_extra_log_files:** This validator checks the EXTRA_LOG_FILES setting to confirm it is a dictionary where keys are valid log levels and values are booleans. - Ensures log levels are valid and that the value for each level is either True or False. - **Error Handling:** Each error is clearly identified with a specific error ID (e.g., django_logging.E022_config_name), making it easier to diagnose and fix configuration issues. - **Improved Reliability:** By adding these validators, misconfigurations related to logging formats and extra log files are detected early, ensuring smoother setup and preventing runtime errors. --- .../validators/config_validators.py | 71 ++++++++++++++++++- 1 file changed, 70 insertions(+), 1 deletion(-) diff --git a/django_logging/validators/config_validators.py b/django_logging/validators/config_validators.py index 8448708..172c26f 100644 --- a/django_logging/validators/config_validators.py +++ b/django_logging/validators/config_validators.py @@ -246,7 +246,76 @@ def validate_integer_setting(value: int, config_name: str) -> List[Error]: Error( f"{config_name} is not a valid integer.", hint=f"Ensure {config_name} is a valid positive integer", - id=f"django_logging.E021_{config_name}", + id=f"django_logging.E019_{config_name}", ) ) return errors + + +def validate_log_file_format_types( + format_types: Dict, + config_name: str, + valid_levels: LogLevels, + valid_formats: List[str], +) -> List[Error]: + errors = [] + if not isinstance(format_types, dict): + errors.append( + Error( + f"{config_name} is not a dictionary.", + hint=f"Ensure {config_name} is a dictionary with log levels as keys.", + id=f"django_logging.E022_{config_name}", + ) + ) + else: + for level, format_type in format_types.items(): + if level not in valid_levels: + errors.append( + Error( + f"Invalid log level '{level}' in {config_name}.", + hint=f"Valid log levels are: {valid_levels}.", + id=f"django_logging.E023_{config_name}", + ) + ) + elif format_type.upper() not in valid_formats: + errors.append( + Error( + f"Invalid log format type '{format_type}' for level '{level}' in {config_name}.", + hint=f"Valid format types are: {valid_formats}.", + id=f"django_logging.E024_{config_name}", + ) + ) + return errors + + +def validate_extra_log_files( + extra_log_files: Dict, config_name: str, valid_levels: LogLevels +) -> List[Error]: + errors = [] + if not isinstance(extra_log_files, dict): + errors.append( + Error( + f"{config_name} is not a dictionary.", + hint=f"Ensure {config_name} is a dictionary with log levels as keys.", + id=f"django_logging.E025_{config_name}", + ) + ) + else: + for level, value in extra_log_files.items(): + if level not in valid_levels: + errors.append( + Error( + f"Invalid log level '{level}' in {config_name}.", + hint=f"Valid log levels are: {valid_levels}.", + id=f"django_logging.E026_{config_name}", + ) + ) + elif not isinstance(value, bool): + errors.append( + Error( + f"Invalid value '{value}' for level '{level}' in {config_name}.", + hint="Value must be either True or False.", + id=f"django_logging.E027_{config_name}", + ) + ) + return errors