Skip to content

Commit

Permalink
⚡✨ feat(validators): Add validators for log file format types and ext…
Browse files Browse the repository at this point in the history
…ra 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.
  • Loading branch information
MEHRSHAD-MIRSHEKARY committed Oct 4, 2024
1 parent 3732fc7 commit 47ebea4
Showing 1 changed file with 70 additions and 1 deletion.
71 changes: 70 additions & 1 deletion django_logging/validators/config_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 47ebea4

Please sign in to comment.