Skip to content

Commit

Permalink
Log level as env var and status logging in main loop (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
elementechemlyn authored Jan 21, 2025
1 parent b7234ef commit 666caea
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/hutch_bunny/core/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@
console_handler = logging.StreamHandler(sys.stdout)
console_handler.setFormatter(LOG_FORMAT)
logger = logging.getLogger(settings.LOGGER_NAME)
logger.setLevel(logging.INFO)
logger.setLevel(settings.LOGGER_LEVEL)
logger.addHandler(console_handler)
2 changes: 2 additions & 0 deletions src/hutch_bunny/core/settings.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import logging
from os import environ
from dotenv import load_dotenv

Expand All @@ -22,6 +23,7 @@

# Logging configuration
LOGGER_NAME = "hutch"
LOGGER_LEVEL = logging.getLevelNamesMapping().get(environ.get("BUNNY_LOGGER_LEVEL"),"INFO")
BACKUP_LOGGER_NAME = "backup"
MSG_FORMAT = "%(levelname)s - %(asctime)s - %(message)s"
DATE_FORMAT = "%d-%b-%y %H:%M:%S"
Expand Down
6 changes: 4 additions & 2 deletions src/hutch_bunny/daemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,11 @@
from hutch_bunny.core.logger import logger
from hutch_bunny.core.setting_database import setting_database


def main() -> None:
# Setting database connection
db_manager = setting_database(logger=logger)
# Task Api Client class init.
client = TaskApiClient()

# Building results modifiers
modifiers_list = results_modifiers(
low_number_suppression_threshold=int(
Expand Down Expand Up @@ -66,5 +64,9 @@ def main() -> None:

elif response.status_code == 204:
logger.info("Looking for job...")
elif response.status_code == 401:
logger.info("Failed to authenticate with task server.")
else:
logger.info("Got http status code: %s", response.status_code)

time.sleep(settings.POLLING_INTERVAL)
22 changes: 22 additions & 0 deletions tests/test_logging_env.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import pytest
import os
import logging
from importlib import reload

import hutch_bunny.core.logger
import hutch_bunny.core.settings

def test_set_level():
os.environ["BUNNY_LOGGER_LEVEL"] = "INFO"
reload(hutch_bunny.core.settings)
reload(hutch_bunny.core.logger)
assert hutch_bunny.core.logger.logger.level == logging.INFO
os.environ["BUNNY_LOGGER_LEVEL"] = "DEBUG"
reload(hutch_bunny.core.settings)
reload(hutch_bunny.core.logger)
assert hutch_bunny.core.logger.logger.level == logging.DEBUG
os.environ["BUNNY_LOGGER_LEVEL"] = "FLOPPSY"
reload(hutch_bunny.core.settings)
reload(hutch_bunny.core.logger)
assert hutch_bunny.core.logger.logger.level == logging.INFO

0 comments on commit 666caea

Please sign in to comment.