diff --git a/app/parser.py b/app/parser.py index 16792e4..7a3df81 100644 --- a/app/parser.py +++ b/app/parser.py @@ -1,6 +1,9 @@ -from lxml import html -import logging import requests +from lxml import html +from constants.logger import logger, log_level, LOG_LEVELS + + +logger.setLevel(LOG_LEVELS[log_level]) def get_anime_filler_list(afl_anime_name: str): @@ -21,7 +24,7 @@ def get_anime_filler_list(afl_anime_name: str): else: fillers.append(int(text)) - if logging.getLogger().getEffectiveLevel() == logging.DEBUG: - logging.debug(filler_ranges) + if logger.getEffectiveLevel() == logger.debug: + logger.debug(filler_ranges) return fillers diff --git a/app/plex.py b/app/plex.py index 84e74a3..8918a27 100644 --- a/app/plex.py +++ b/app/plex.py @@ -1,14 +1,14 @@ -import logging - from plexapi.video import Episode from plexapi.server import PlexServer from plexapi.base import MediaContainer from plexapi.library import ShowSection from constants.variables import UserConfig +from constants.logger import logger, log_level, LOG_LEVELS + +logger.setLevel(LOG_LEVELS[log_level]) var = UserConfig() -logging.basicConfig(level=logging.INFO) plex = PlexServer(baseurl=var.plex_url, token=var.plex_token) @@ -43,4 +43,4 @@ def create_plex_collection(sonarr_episodes: list[int], fillers: list[int] = []): items=items, ) else: - logging.info(f"No {collection_name.split(' - ')[1].lower()} found.") + logger.info(f"No {collection_name.split(' - ')[1].lower()} found.") diff --git a/app/sonarr.py b/app/sonarr.py index 22c42ba..afd82ee 100644 --- a/app/sonarr.py +++ b/app/sonarr.py @@ -1,7 +1,10 @@ import sonarr -import logging from constants.variables import UserConfig +from constants.logger import logger, log_level, LOG_LEVELS + + +logger.setLevel(LOG_LEVELS[log_level]) var = UserConfig() @@ -27,12 +30,12 @@ def get_sonarr_episodes(series_id: int): "season": item.season_number, "monitored": item.monitored, "episode_number": item.episode_number, - "absolute_episode_number": item.absolute_episode_number + "absolute_episode_number": item.absolute_episode_number, } ) except Exception as e: - logging.error("Exception when calling EpisodeApi->list_episode: %s\n" % e) + logger.error("Exception when calling EpisodeApi->list_episode: %s\n" % e) return episodes @@ -52,6 +55,6 @@ def configure_monitoring(monitored_list: list): episodes_monitored_resource=episodes_monitored_resource ) except Exception as e: - logging.error( + logger.error( "Exception when calling EpisodeApi->put_episode_monitor: %s\n" % e ) diff --git a/constants/logger.py b/constants/logger.py index bf87e00..e92739a 100644 --- a/constants/logger.py +++ b/constants/logger.py @@ -1,9 +1,18 @@ +import os import logging -LOG_LEVELS: dict[str, int] = { +LOG_LEVELS = { "DEBUG": logging.DEBUG, "INFO": logging.INFO, "WARNING": logging.WARNING, "ERROR": logging.ERROR, "CRITICAL": logging.CRITICAL, } + +log_level = os.environ.get("LOG_LEVEL", "INFO").upper() +if log_level not in LOG_LEVELS: + print(f"Invalid log level: {log_level}. Defaulting to INFO.") + log_level = "INFO" + +logger = logging.getLogger(__name__) +logger.setLevel(LOG_LEVELS[log_level]) diff --git a/constants/variables.py b/constants/variables.py index 3c45f33..52e347f 100644 --- a/constants/variables.py +++ b/constants/variables.py @@ -1,13 +1,13 @@ import os import sys -import logging from dotenv import load_dotenv +from constants.logger import logger, LOG_LEVELS -load_dotenv(dotenv_path=".env.priv") +logger.setLevel(LOG_LEVELS["DEBUG"]) -logging.basicConfig(level=logging.INFO) +load_dotenv(dotenv_path=".env.priv") class UserConfig: @@ -58,10 +58,10 @@ def _get_env_var( """Helper method to get and validate environment variables.""" value = os.environ.get(key, default) if required and value is None: - logging.error(f"{key} must be set") + logger.error(f"{key} must be set") sys.exit(1) try: return value except ValueError: - logging.error(f"Invalid value for {key}: {value}") + logger.error(f"Invalid value for {key}: {value}") sys.exit(1) diff --git a/main.py b/main.py index 44dd986..034b8a2 100644 --- a/main.py +++ b/main.py @@ -1,32 +1,21 @@ -import os -import sys -import logging - - from app.parser import get_anime_filler_list from app.sonarr import configure_monitoring, get_sonarr_episodes from app.plex import create_plex_collection -from constants.logger import LOG_LEVELS from constants.variables import UserConfig +from constants.logger import logger, log_level, LOG_LEVELS -var = UserConfig() +logger.setLevel(LOG_LEVELS[log_level]) -log_level = os.environ.get("LOG_LEVEL", "INFO").upper() - -logging.basicConfig( - format="%(asctime)s - %(levelname)s - %(message)s", - level=LOG_LEVELS.get(log_level, logging.INFO), - handlers=[logging.StreamHandler(sys.stdout)], -) +var = UserConfig() if __name__ == "__main__": episodes_to_monitor = [] - logging.info("Initializing SoFE...") - logging.info("Address is set to '%s'", var.listen_address) - logging.info("Port is set to '%s'", var.listen_port) + logger.info("Initializing SoFE...") + logger.info("Address is set to '%s'", var.listen_address) + logger.info("Port is set to '%s'", var.listen_port) fillers_from_api = get_anime_filler_list(var.afl_anime_name) sonarr_episodes_id = get_sonarr_episodes(int(var.sonarr_series_id)) @@ -40,7 +29,7 @@ if episode["episode_number"] not in fillers_from_api: episodes_to_monitor.append(episode.get("id")) - logging.debug("Non-Filler Episodes: %s", episodes_to_monitor) + logger.debug("Non-Filler Episodes: %s", episodes_to_monitor) if not episodes_to_monitor or var.monitor_non_filler_sonarr_episodes is True: configure_monitoring(monitored_list=episodes_to_monitor)