Skip to content

Commit

Permalink
✨⚡ feat(constants): add default_settings & type annotations file
Browse files Browse the repository at this point in the history
- Introduced default settings for logging configurations, such as LOG_DIR, LOG_FILE_LEVELS, and LOG_DATE_FORMAT.
- Refactored the package to include config types using type annotations to improve code clarity and maintainability.

This commit enhances the robustness and readability of the Django logging package, making it easier to maintain and extend.
  • Loading branch information
MEHRSHAD-MIRSHEKARY committed Aug 20, 2024
1 parent ddcab0c commit 075e805
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 1 deletion.
2 changes: 1 addition & 1 deletion django_logging/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class DjangoLoggingConfig(AppConfig):
name = "django_logging"
verbose_name = _("Django Logging")

def ready(self):
def ready(self) -> None:
from django_logging.utils.setup_logging import set_logging
from django_logging.utils.get_config import get_conf
conf = get_conf()
Expand Down
11 changes: 11 additions & 0 deletions django_logging/constants/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,12 @@
from .format_options import FORMAT_OPTIONS
from .default_settings import (
DEFAULT_INITIALIZATION_MESSAGE_ENABLED,
DEFAULT_LOG_FILE_FORMATS,
DEFAULT_LOG_CONSOLE_COLORIZE,
DEFAULT_LOG_CONSOLE_FORMAT,
DEFAULT_LOG_CONSOLE_LEVEL,
DEFAULT_LOG_DATE_FORMAT,
DEFAULT_LOG_DIR,
DEFAULT_LOG_EMAIL_NOTIFIER,
DEFAULT_LOG_FILE_LEVELS,
)
53 changes: 53 additions & 0 deletions django_logging/constants/default_settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import os

from django_logging.constants.config_types import (
LogFileFormatsType,
LOG_DIR_TYPE,
LOG_FILE_LEVELS_TYPE,
LOG_CONSOLE_FORMAT_TYPE,
LOG_CONSOLE_LEVEL_TYPE,
LOG_CONSOLE_COLORIZE_TYPE,
LOG_DATE_FORMAT_TYPE,
INITIALIZATION_MESSAGE_ENABLED_TYPE,
LogEmailNotifierType,
)

# Default directory for logs
DEFAULT_LOG_DIR: LOG_DIR_TYPE = os.path.join(os.getcwd(), "logs")

# Default log levels
DEFAULT_LOG_FILE_LEVELS: LOG_FILE_LEVELS_TYPE = [
"DEBUG",
"INFO",
"WARNING",
"ERROR",
"CRITICAL",
]


# Default log date format
DEFAULT_LOG_DATE_FORMAT: LOG_DATE_FORMAT_TYPE = "%Y-%m-%d %H:%M:%S"

DEFAULT_INITIALIZATION_MESSAGE_ENABLED: INITIALIZATION_MESSAGE_ENABLED_TYPE = True

DEFAULT_LOG_FILE_FORMATS: LogFileFormatsType = {
"DEBUG": 1,
"INFO": 1,
"WARNING": 1,
"ERROR": 1,
"CRITICAL": 1,
}

DEFAULT_LOG_CONSOLE_LEVEL: LOG_CONSOLE_LEVEL_TYPE = "DEBUG"

DEFAULT_LOG_CONSOLE_FORMAT: LOG_CONSOLE_FORMAT_TYPE = 1

DEFAULT_LOG_CONSOLE_COLORIZE: LOG_CONSOLE_COLORIZE_TYPE = True

DEFAULT_LOG_EMAIL_NOTIFIER: LogEmailNotifierType = {
"ENABLE": False,
"NOTIFY_ERROR": False,
"NOTIFY_CRITICAL": False,
"LOG_FORMAT": 1,
"USE_TEMPLATE": True,
}
32 changes: 32 additions & 0 deletions django_logging/constants/settings_types.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from typing import TypedDict, Union, List, Literal

FormatOption = Union[int, str]


class LogEmailNotifierType(TypedDict, total=False):
ENABLE: bool
NOTIFY_ERROR: bool
NOTIFY_CRITICAL: bool
LOG_FORMAT: FormatOption
USE_TEMPLATE: bool


LogLevel = Literal["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"]


class LogFileFormatsType(TypedDict, total=False):
DEBUG: FormatOption
INFO: FormatOption
WARNING: FormatOption
ERROR: FormatOption
CRITICAL: FormatOption


# Type Aliases for other configurations
LOG_DIR_TYPE = str
LOG_FILE_LEVELS_TYPE = List[str]
LOG_DATE_FORMAT_TYPE = str
INITIALIZATION_MESSAGE_ENABLED_TYPE = bool
LOG_CONSOLE_LEVEL_TYPE = LogLevel
LOG_CONSOLE_FORMAT_TYPE = FormatOption
LOG_CONSOLE_COLORIZE_TYPE = bool

0 comments on commit 075e805

Please sign in to comment.