Skip to content

Commit

Permalink
Merge pull request #13 from MEHRSHAD-MIRSHEKARY/feature/optional-formats
Browse files Browse the repository at this point in the history
Feature/optional formats
  • Loading branch information
ARYAN-NIKNEZHAD authored Aug 18, 2024
2 parents 69e2b5a + d773ea3 commit 4a42240
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 12 deletions.
17 changes: 16 additions & 1 deletion django_logging/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,27 @@ def ready(self):
"LOG_FILE_LEVELS", ["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"]
)
log_dir = log_settings.get("LOG_DIR", os.path.join(os.getcwd(), "logs"))
log_file_formats = log_settings.get("LOG_FILE_FORMATS", {})
console_level = log_settings.get("LOG_CONSOLE_LEVEL", "DEBUG")
console_format = log_settings.get("LOG_CONSOLE_FORMAT")
log_date_format = log_settings.get("LOG_DATE_FORMAT", "%Y-%m-%d %H:%M:%S")
log_email_notifier = log_settings.get("LOG_EMAIL_NOTIFIER", {})
log_email_notifier_enable = log_email_notifier.get("ENABLE", False)
log_email_notifier_log_levels = [
"ERROR" if log_email_notifier.get("NOTIFY_ERROR", False) else None,
"CRITICAL" if log_email_notifier.get("NOTIFY_CRITICAL", False) else None,
]
log_email_notifier_log_format = log_email_notifier.get("LOG_FORMAT")

# Set the logging configuration
set_logging(log_levels, log_dir, log_email_notifier_enable, log_email_notifier_log_levels)
set_logging(
log_levels,
log_dir,
log_file_formats,
console_level,
console_format,
log_date_format,
log_email_notifier_enable,
log_email_notifier_log_levels,
log_email_notifier_log_format
)
1 change: 1 addition & 0 deletions django_logging/constants/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .format_options import FORMAT_OPTIONS
15 changes: 15 additions & 0 deletions django_logging/constants/format_options.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
FORMAT_OPTIONS = {
1: "%(levelname)s | %(asctime)s | %(module)s | %(message)s",
2: "%(levelname)s | %(asctime)s | %(message)s",
3: "%(levelname)s | %(message)s",
4: "%(asctime)s - %(name)s - %(levelname)s - %(message)s",
5: "%(levelname)s | %(message)s | [in %(pathname)s:%(lineno)d]",
6: "%(asctime)s | %(levelname)s | %(message)s",
7: "%(levelname)s | %(asctime)s | in %(module)s: %(message)s",
8: "%(levelname)s | %(message)s | [%(filename)s:%(lineno)d]",
9: "[%(asctime)s] | %(levelname)s | in %(module)s: %(message)s",
10: "%(asctime)s | %(processName)s | %(name)s | %(levelname)s | %(message)s",
11: "%(asctime)s | %(threadName)s | %(name)s | %(levelname)s | %(message)s",
12: "%(levelname)s | [%(asctime)s] | (%(filename)s:%(lineno)d) | %(message)s",
13: "%(levelname)s | [%(asctime)s] | {%(name)s} | (%(filename)s:%(lineno)d): %(message)s",
}
68 changes: 63 additions & 5 deletions django_logging/settings/conf.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import logging
import logging.config
import os
from typing import List, Dict, Optional
from typing import List, Dict, Optional, Union

from django_logging.constants import FORMAT_OPTIONS
from django_logging.filters.level_filter import LoggingLevelFilter


Expand All @@ -19,14 +20,54 @@ def __init__(
self,
log_levels: List[str],
log_dir: str,
log_file_formats: Dict[str, Union[int, str]],
console_level: str,
console_format: Optional[Union[int, str]],
log_date_format: str,
log_email_notifier_enable: bool,
log_email_notifier_log_levels: List[str],
log_email_notifier_log_format: Union[int, str],
) -> None:

self.log_levels = log_levels
self.log_dir = log_dir
self.log_file_formats = self._resolve_file_formats(log_file_formats)
self.log_date_format = log_date_format
self.console_level = console_level
self.console_format = self.resolve_format(console_format)
self.email_notifier_enable = log_email_notifier_enable
self.email_notifier_log_levels = log_email_notifier_log_levels
self.email_notifier_log_format = self.resolve_format(
log_email_notifier_log_format
)

def _resolve_file_formats(self, log_file_formats: Dict[str, Union[int, str]]) -> Dict:
resolved_formats = {}
for level in self.log_levels:
format_option = log_file_formats.get(level, None)
if format_option:
if isinstance(format_option, int):
resolved_formats[level] = FORMAT_OPTIONS.get(
format_option, FORMAT_OPTIONS[1]
)
else:
resolved_formats[level] = format_option
else:
resolved_formats[level] = FORMAT_OPTIONS[1]

return resolved_formats

@staticmethod
def resolve_format(_format: Union[int, str]) -> str:
if _format:
if isinstance(_format, int):
resolved_format = FORMAT_OPTIONS.get(_format, FORMAT_OPTIONS[1])
else:
resolved_format = _format
else:
resolved_format = FORMAT_OPTIONS[1]

return resolved_format


class LogManager:
Expand Down Expand Up @@ -72,7 +113,7 @@ def set_conf(self) -> None:
level.lower(): {
"class": "logging.FileHandler",
"filename": log_file,
"formatter": "default",
"formatter": f"{level.lower()}",
"level": level,
"filters": [level.lower()]
}
Expand All @@ -81,19 +122,18 @@ def set_conf(self) -> None:
handlers["console"] = {
"class": "logging.StreamHandler",
"formatter": "console",
"level": "DEBUG",
"level": self.log_config.console_level,
}

email_handler = {
f"email_{level.lower()}": {
"class": "django_logging.handlers.EmailHandler",
"formatter": "email",
"level": level,
"filters": [level.lower()],
}
for level in self.log_config.email_notifier_log_levels
if level
}

if self.log_config.email_notifier_enable:
handlers.update(email_handler)

Expand All @@ -105,6 +145,23 @@ def set_conf(self) -> None:
for level in self.log_config.log_levels
}

formatters = {
level.lower(): {
"format": self.log_config.log_file_formats[level],
"datefmt": self.log_config.log_date_format,
}
for level in self.log_config.log_levels
}
formatters["console"] = {
"format": self.log_config.console_format,
"datefmt": self.log_config.log_date_format,
}

formatters["email"] = {
"format": self.log_config.email_notifier_log_format,
"datefmt": self.log_config.log_date_format,
}

loggers = {
level.lower(): {
"level": level,
Expand All @@ -116,6 +173,7 @@ def set_conf(self) -> None:

config = {
"version": 1,
"formatters": formatters,
"handlers": handlers,
"filters": filters,
"loggers": loggers,
Expand Down
29 changes: 23 additions & 6 deletions django_logging/utils/setup_conf.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,37 @@
from django_logging.settings.conf import LogConfig, LogManager

from typing import List
from typing import List, Optional, Union, Dict


def set_logging(log_levels: List[str],
log_dir: str,
log_email_notifier_enable: bool,
log_email_notifier_log_levels: List[str]):
def set_logging(
log_levels: List[str],
log_dir: str,
log_file_formats: Dict[str, Union[int, str]],
console_level: str,
console_format: Optional[Union[int, str]],
log_date_format: str,
log_email_notifier_enable: bool,
log_email_notifier_log_levels: List[str],
log_email_notifier_log_format: Union[int, str]
):
"""
Sets up the logging configuration.
Args:
log_levels (List[str]): A list of log levels to configure.
log_dir (str): The directory where log files will be stored.
"""
log_config = LogConfig(log_levels, log_dir, log_email_notifier_enable, log_email_notifier_log_levels)
log_config = LogConfig(
log_levels,
log_dir,
log_file_formats,
console_level,
console_format,
log_date_format,
log_email_notifier_enable,
log_email_notifier_log_levels,
log_email_notifier_log_format
)
log_manager = LogManager(log_config)
log_manager.create_log_files()
log_manager.set_conf()

0 comments on commit 4a42240

Please sign in to comment.