Skip to content

Commit

Permalink
testing out logging
Browse files Browse the repository at this point in the history
  • Loading branch information
chkpwd committed Oct 25, 2024
1 parent 646e9c1 commit 36001e5
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 36 deletions.
11 changes: 7 additions & 4 deletions app/parser.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -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
8 changes: 4 additions & 4 deletions app/plex.py
Original file line number Diff line number Diff line change
@@ -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)

Expand Down Expand Up @@ -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.")
11 changes: 7 additions & 4 deletions app/sonarr.py
Original file line number Diff line number Diff line change
@@ -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()

Expand All @@ -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

Expand All @@ -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
)
11 changes: 10 additions & 1 deletion constants/logger.py
Original file line number Diff line number Diff line change
@@ -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])
10 changes: 5 additions & 5 deletions constants/variables.py
Original file line number Diff line number Diff line change
@@ -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:
Expand Down Expand Up @@ -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)
25 changes: 7 additions & 18 deletions main.py
Original file line number Diff line number Diff line change
@@ -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))
Expand All @@ -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)

0 comments on commit 36001e5

Please sign in to comment.