From ce2e6314374b9f571fb9c28e94d4cf3f26de35b1 Mon Sep 17 00:00:00 2001 From: sanj <67624670+iodrift@users.noreply.github.com> Date: Mon, 5 Aug 2024 00:03:33 -0700 Subject: [PATCH] Auto-update: Mon Aug 5 00:03:33 PDT 2024 --- sijapi/__init__.py | 3 +-- sijapi/classes.py | 54 +++++++++++++++++++++++++++++++++++++++------- sijapi/logs.py | 42 ------------------------------------ 3 files changed, 47 insertions(+), 52 deletions(-) delete mode 100644 sijapi/logs.py diff --git a/sijapi/__init__.py b/sijapi/__init__.py index b945fd9..981296a 100644 --- a/sijapi/__init__.py +++ b/sijapi/__init__.py @@ -6,8 +6,7 @@ import multiprocessing from dotenv import load_dotenv from dateutil import tz from pathlib import Path -from .classes import Geocoder, APIConfig, Configuration -from .logs import Logger +from .classes import Geocoder, APIConfig, Configuration, Logger # INITIALization BASE_DIR = Path(__file__).resolve().parent diff --git a/sijapi/classes.py b/sijapi/classes.py index efb39d7..3b7886a 100644 --- a/sijapi/classes.py +++ b/sijapi/classes.py @@ -24,26 +24,65 @@ from datetime import datetime, timedelta, timezone from timezonefinder import TimezoneFinder from zoneinfo import ZoneInfo from srtm import get_data -from .logs import Logger +import os +import sys +from loguru import logger + +# Custom logger class +class Logger: + def __init__(self, name, logs_dir): + self.logs_dir = logs_dir + self.name = name + self.logger = logger + self.debug_modules = set() + + def setup_from_args(self, args): + if not os.path.exists(self.logs_dir): + os.makedirs(self.logs_dir) + + self.logger.remove() + + log_format = "{time:YYYY-MM-DD HH:mm:ss} - {name} - {level: <8} - {message}" + + # File handler + self.logger.add(os.path.join(self.logs_dir, 'app.log'), rotation="2 MB", level="DEBUG", format=log_format) + + # Set debug modules + self.debug_modules = set(args.debug) + + # Console handler with custom filter + def module_filter(record): + return (record["level"].no >= logger.level(args.log.upper()).no or + record["name"] in self.debug_modules) + + self.logger.add(sys.stdout, level="DEBUG", format=log_format, filter=module_filter, colorize=True) + + # Custom color and style mappings + self.logger.level("CRITICAL", color="") + self.logger.level("ERROR", color="") + self.logger.level("WARNING", color="") + self.logger.level("DEBUG", color="") + + self.logger.info(f"Debug modules: {self.debug_modules}") + + def get_module_logger(self, module_name): + return self.logger.bind(name=module_name) L = Logger("classes", "classes") logger = L.get_module_logger("classes") - -# Logging functions def debug(text: str): logger.debug(text) def info(text: str): logger.info(text) def warn(text: str): logger.warning(text) def err(text: str): logger.error(text) def crit(text: str): logger.critical(text) - -T = TypeVar('T', bound='Configuration') - BASE_DIR = Path(__file__).resolve().parent CONFIG_DIR = BASE_DIR / "config" ENV_PATH = CONFIG_DIR / ".env" load_dotenv(ENV_PATH) TS_ID = os.environ.get('TS_ID') +T = TypeVar('T', bound='Configuration') +# Primary configuration class class Configuration(BaseModel): HOME: Path = Path.home() _dir_config: Optional['Configuration'] = None @@ -87,7 +126,6 @@ class Configuration(BaseModel): err(f"Error loading configuration: {str(e)}") raise - @classmethod def _resolve_path(cls, path: Union[str, Path], default_dir: str) -> Path: base_path = Path(__file__).parent.parent # This will be two levels up from this file @@ -168,7 +206,7 @@ class Configuration(BaseModel): arbitrary_types_allowed = True - +# Configuration class for API & Database methods. class APIConfig(BaseModel): HOST: str PORT: int diff --git a/sijapi/logs.py b/sijapi/logs.py deleted file mode 100644 index e852c5c..0000000 --- a/sijapi/logs.py +++ /dev/null @@ -1,42 +0,0 @@ -import os -import sys -from loguru import logger - -class Logger: - def __init__(self, name, logs_dir): - self.logs_dir = logs_dir - self.name = name - self.logger = logger - self.debug_modules = set() - - def setup_from_args(self, args): - if not os.path.exists(self.logs_dir): - os.makedirs(self.logs_dir) - - self.logger.remove() - - log_format = "{time:YYYY-MM-DD HH:mm:ss} - {name} - {level: <8} - {message}" - - # File handler - self.logger.add(os.path.join(self.logs_dir, 'app.log'), rotation="2 MB", level="DEBUG", format=log_format) - - # Set debug modules - self.debug_modules = set(args.debug) - - # Console handler with custom filter - def module_filter(record): - return (record["level"].no >= logger.level(args.log.upper()).no or - record["name"] in self.debug_modules) - - self.logger.add(sys.stdout, level="DEBUG", format=log_format, filter=module_filter, colorize=True) - - # Custom color and style mappings - self.logger.level("CRITICAL", color="") - self.logger.level("ERROR", color="") - self.logger.level("WARNING", color="") - self.logger.level("DEBUG", color="") - - self.logger.info(f"Debug modules: {self.debug_modules}") - - def get_module_logger(self, module_name): - return self.logger.bind(name=module_name)