From 3442557019ba02084e4b07abe116e22b745d78db Mon Sep 17 00:00:00 2001 From: sean roberts Date: Sun, 27 Oct 2024 12:19:33 -0700 Subject: [PATCH] moves log functions to logging.py - updates tests and code to use src/logging - exclude log files --- .gitignore | 8 +++--- main.py | 2 +- src/aihawk_authenticator.py | 2 +- src/aihawk_bot_facade.py | 2 +- src/aihawk_easy_applier.py | 2 +- src/aihawk_job_manager.py | 2 +- src/job.py | 2 +- src/job_application_profile.py | 2 +- src/llm/llm_manager.py | 2 +- src/logging.py | 46 ++++++++++++++++++++++++++++++++ src/utils.py | 46 ++------------------------------ tests/test_aihawk_job_manager.py | 2 +- tests/test_utils.py | 2 +- 13 files changed, 63 insertions(+), 57 deletions(-) create mode 100644 src/logging.py diff --git a/.gitignore b/.gitignore index 9b53de8b0..4938272a5 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,8 @@ +# application files and logs +/generated_cv +/data_folder/secrets.yaml +/log/* + # Byte-compiled / optimized / DLL files __pycache__/ *.py[cod] @@ -159,6 +164,3 @@ venv.bak/ # Mono Auto Generated Files mono_crash.* - -/generated_cv -data_folder/secrets.yaml diff --git a/main.py b/main.py index 82c66f0c7..fa3ae5bd3 100644 --- a/main.py +++ b/main.py @@ -15,7 +15,7 @@ from src.aihawk_bot_facade import AIHawkBotFacade from src.aihawk_job_manager import AIHawkJobManager from src.job_application_profile import JobApplicationProfile -from loguru import logger +from src.logging import logger # Suppress stderr only during specific operations original_stderr = sys.stderr diff --git a/src/aihawk_authenticator.py b/src/aihawk_authenticator.py index e729f96ed..9485e796a 100644 --- a/src/aihawk_authenticator.py +++ b/src/aihawk_authenticator.py @@ -6,7 +6,7 @@ from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.support.ui import WebDriverWait -from loguru import logger +from src.logging import logger class AIHawkAuthenticator: diff --git a/src/aihawk_bot_facade.py b/src/aihawk_bot_facade.py index 091a46d6b..1952a5108 100644 --- a/src/aihawk_bot_facade.py +++ b/src/aihawk_bot_facade.py @@ -1,4 +1,4 @@ -from loguru import logger +from src.logging import logger class AIHawkBotState: diff --git a/src/aihawk_easy_applier.py b/src/aihawk_easy_applier.py index 5874c8ed1..e57699d87 100644 --- a/src/aihawk_easy_applier.py +++ b/src/aihawk_easy_applier.py @@ -20,7 +20,7 @@ from selenium.webdriver.support.ui import Select, WebDriverWait import src.utils as utils -from loguru import logger +from src.logging import logger class AIHawkEasyApplier: diff --git a/src/aihawk_job_manager.py b/src/aihawk_job_manager.py index 4e61e7470..fb69b9500 100644 --- a/src/aihawk_job_manager.py +++ b/src/aihawk_job_manager.py @@ -13,7 +13,7 @@ from app_config import MINIMUM_WAIT_TIME from src.job import Job from src.aihawk_easy_applier import AIHawkEasyApplier -from loguru import logger +from src.logging import logger import urllib.parse diff --git a/src/job.py b/src/job.py index ff72d4702..3bcd9937a 100644 --- a/src/job.py +++ b/src/job.py @@ -1,6 +1,6 @@ from dataclasses import dataclass -from loguru import logger +from src.logging import logger @dataclass diff --git a/src/job_application_profile.py b/src/job_application_profile.py index 5b9ea8e94..8a74bdb7e 100644 --- a/src/job_application_profile.py +++ b/src/job_application_profile.py @@ -2,7 +2,7 @@ import yaml -from loguru import logger +from src.logging import logger @dataclass diff --git a/src/llm/llm_manager.py b/src/llm/llm_manager.py index f9162e5a3..2eba2af90 100644 --- a/src/llm/llm_manager.py +++ b/src/llm/llm_manager.py @@ -19,7 +19,7 @@ from langchain_core.prompts import ChatPromptTemplate import src.strings as strings -from loguru import logger +from src.logging import logger load_dotenv() diff --git a/src/logging.py b/src/logging.py new file mode 100644 index 000000000..e03e3c869 --- /dev/null +++ b/src/logging.py @@ -0,0 +1,46 @@ +import os +import random +import sys +import time +from selenium import webdriver +from loguru import logger +from app_config import MINIMUM_LOG_LEVEL, LOG_TO_FILE, LOG_TO_CONSOLE + +from selenium.webdriver.remote.remote_connection import LOGGER as selenium_logger +selenium_logger.setLevel(MINIMUM_LOG_LEVEL) + +log_file = "log/app.log" + +# Ensure the log directory exists +os.makedirs(os.path.dirname(log_file), exist_ok=True) + +# Remove default logger +logger.remove() + +# Configure Loguru logger +config = { + "handlers": [] +} + +# Add file logger if LOG_TO_FILE is True +if LOG_TO_FILE: + config["handlers"].append({ + "sink": log_file, + "level": MINIMUM_LOG_LEVEL, + "rotation": "10 MB", + "retention": "1 week", + "compression": "zip", + "format": "{time:YYYY-MM-DD HH:mm:ss.SSS} | {level: <8} | {name}:{function}:{line} - {message}" + }) + +# Add console logger if LOG_TO_CONSOLE is True +if LOG_TO_CONSOLE: + config["handlers"].append({ + "sink": sys.stderr, + "level": MINIMUM_LOG_LEVEL, + "format": "{time:YYYY-MM-DD HH:mm:ss.SSS} | {level: <8} | {name}:{function}:{line} - {message}" + }) + +# Configure Loguru with the new settings +logger.configure(**config) + diff --git a/src/utils.py b/src/utils.py index 2a79d775c..8d5ca6cb0 100644 --- a/src/utils.py +++ b/src/utils.py @@ -1,50 +1,8 @@ import os -import random -import sys import time +import random from selenium import webdriver -from loguru import logger - -from app_config import MINIMUM_LOG_LEVEL, LOG_TO_FILE, LOG_TO_CONSOLE - -log_file = "log/app.log" - -# Ensure the log directory exists -os.makedirs(os.path.dirname(log_file), exist_ok=True) - -# Remove default logger -logger.remove() - -# Configure Loguru logger -config = { - "handlers": [] -} - -# Add file logger if LOG_TO_FILE is True -if LOG_TO_FILE: - config["handlers"].append({ - "sink": log_file, - "level": MINIMUM_LOG_LEVEL, - "rotation": "10 MB", - "retention": "1 week", - "compression": "zip", - "format": "{time:YYYY-MM-DD HH:mm:ss.SSS} | {level: <8} | {name}:{function}:{line} - {message}" - }) - -# Add console logger if LOG_TO_CONSOLE is True -if LOG_TO_CONSOLE: - config["handlers"].append({ - "sink": sys.stderr, - "level": MINIMUM_LOG_LEVEL, - "format": "{time:YYYY-MM-DD HH:mm:ss.SSS} | {level: <8} | {name}:{function}:{line} - {message}" - }) - -# Configure Loguru with the new settings -logger.configure(**config) - -# Intercept Selenium's logging -from selenium.webdriver.remote.remote_connection import LOGGER as selenium_logger -selenium_logger.setLevel(MINIMUM_LOG_LEVEL) +from src.logging import logger chromeProfilePath = os.path.join(os.getcwd(), "chrome_profile", "linkedin_profile") diff --git a/tests/test_aihawk_job_manager.py b/tests/test_aihawk_job_manager.py index 00d77629f..9701626f3 100644 --- a/tests/test_aihawk_job_manager.py +++ b/tests/test_aihawk_job_manager.py @@ -5,7 +5,7 @@ import pytest from src.aihawk_job_manager import AIHawkJobManager from selenium.common.exceptions import NoSuchElementException -from loguru import logger +from src.logging import logger @pytest.fixture diff --git a/tests/test_utils.py b/tests/test_utils.py index 621be61be..ba8701ba4 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -9,7 +9,7 @@ # Mocking logging to avoid actual file writing @pytest.fixture(autouse=True) def mock_logger(mocker): - mocker.patch("src.utils.logger") + mocker.patch("src.logging.logger") # Test ensure_chrome_profile function def test_ensure_chrome_profile(mocker):