Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

file based logging #598

Merged
merged 8 commits into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# application files and logs
/generated_cv
/data_folder/secrets.yaml
/log/*

# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
Expand Down Expand Up @@ -159,6 +164,3 @@ venv.bak/

# Mono Auto Generated Files
mono_crash.*

/generated_cv
data_folder/secrets.yaml
2 changes: 2 additions & 0 deletions app_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,7 @@
- "CRITICAL"
"""
MINIMUM_LOG_LEVEL = "DEBUG"
LOG_TO_FILE = True
LOG_TO_CONSOLE = True

MINIMUM_WAIT_TIME = 60
2 changes: 1 addition & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from src.utils import chrome_browser_options

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
Expand Down
2 changes: 1 addition & 1 deletion src/ai_hawk/authenticator.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait

from loguru import logger
from src.logging import logger

def get_authenticator(driver, platform):
if platform == 'linkedin':
Expand Down
2 changes: 1 addition & 1 deletion src/ai_hawk/bot_facade.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from loguru import logger
from src.logging import logger


class AIHawkBotState:
Expand Down
2 changes: 1 addition & 1 deletion src/ai_hawk/job_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
import src.utils as utils
from app_config import MINIMUM_WAIT_TIME
from src.job import Job
from src.logging import logger

from loguru import logger
import urllib.parse


Expand Down
6 changes: 1 addition & 5 deletions src/ai_hawk/linkedIn_easy_applier.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -176,7 +176,6 @@ def _find_easy_apply_button(self, job: Any) -> WebElement:
]

while attempt < 2:

self.check_for_premium_redirect(job)
self._scroll_page()

Expand All @@ -185,12 +184,10 @@ def _find_easy_apply_button(self, job: Any) -> WebElement:
logger.debug(f"Attempting search using {method['description']}")

if method.get('find_elements'):

buttons = self.driver.find_elements(By.XPATH, method['xpath'])
if buttons:
for index, button in enumerate(buttons):
try:

WebDriverWait(self.driver, 10).until(EC.visibility_of(button))
WebDriverWait(self.driver, 10).until(EC.element_to_be_clickable(button))
logger.debug(f"Found 'Easy Apply' button {index + 1}, attempting to click")
Expand All @@ -200,7 +197,6 @@ def _find_easy_apply_button(self, job: Any) -> WebElement:
else:
raise TimeoutException("No 'Easy Apply' buttons found")
else:

button = WebDriverWait(self.driver, 10).until(
EC.presence_of_element_located((By.XPATH, method['xpath']))
)
Expand Down
2 changes: 1 addition & 1 deletion src/ai_hawk/llm/llm_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
2 changes: 1 addition & 1 deletion src/job.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from dataclasses import dataclass

from loguru import logger
from src.logging import logger


@dataclass
Expand Down
2 changes: 1 addition & 1 deletion src/job_application_profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import yaml

from loguru import logger
from src.logging import logger


@dataclass
Expand Down
46 changes: 46 additions & 0 deletions src/logging.py
Original file line number Diff line number Diff line change
@@ -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": "<green>{time:YYYY-MM-DD HH:mm:ss.SSS}</green> | <level>{level: <8}</level> | <cyan>{name}</cyan>:<cyan>{function}</cyan>:<cyan>{line}</cyan> - <level>{message}</level>"
})

# Add console logger if LOG_TO_CONSOLE is True
if LOG_TO_CONSOLE:
config["handlers"].append({
"sink": sys.stderr,
"level": MINIMUM_LOG_LEVEL,
"format": "<green>{time:YYYY-MM-DD HH:mm:ss.SSS}</green> | <level>{level: <8}</level> | <cyan>{name}</cyan>:<cyan>{function}</cyan>:<cyan>{line}</cyan> - <level>{message}</level>"
})

# Configure Loguru with the new settings
logger.configure(**config)

25 changes: 2 additions & 23 deletions src/utils.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,8 @@
import logging
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_file = "app_log.log"


if MINIMUM_LOG_LEVEL in ["DEBUG", "TRACE", "INFO", "WARNING", "ERROR", "CRITICAL"]:
logger.remove()
logger.add(sys.stderr, level=MINIMUM_LOG_LEVEL)
else:
logger.warning(f"Invalid log level: {MINIMUM_LOG_LEVEL}. Defaulting to DEBUG.")
logger.remove()
logger.add(sys.stderr, level="DEBUG")
from src.logging import logger

chromeProfilePath = os.path.join(os.getcwd(), "chrome_profile", "linkedin_profile")

Expand All @@ -33,15 +17,13 @@ def ensure_chrome_profile():
logger.debug(f"Created Chrome profile directory: {chromeProfilePath}")
return chromeProfilePath


def is_scrollable(element):
scroll_height = element.get_attribute("scrollHeight")
client_height = element.get_attribute("clientHeight")
scrollable = int(scroll_height) > int(client_height)
logger.debug(f"Element scrollable check: scrollHeight={scroll_height}, clientHeight={client_height}, scrollable={scrollable}")
return scrollable


def scroll_slow(driver, scrollable_element, start=0, end=3600, step=300, reverse=False):
logger.debug(f"Starting slow scroll: start={start}, end={end}, step={step}, reverse={reverse}")

Expand Down Expand Up @@ -110,7 +92,6 @@ def scroll_slow(driver, scrollable_element, start=0, end=3600, step=300, reverse
except Exception as e:
logger.error(f"Exception occurred during scrolling: {e}")


def chrome_browser_options():
logger.debug("Setting Chrome browser options")
ensure_chrome_profile()
Expand Down Expand Up @@ -153,14 +134,12 @@ def chrome_browser_options():

return options


def printred(text):
red = "\033[91m"
reset = "\033[0m"
logger.debug("Printing text in red: %s", text)
print(f"{red}{text}{reset}")


def printyellow(text):
yellow = "\033[93m"
reset = "\033[0m"
Expand Down
2 changes: 1 addition & 1 deletion tests/test_aihawk_job_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import pytest
from ai_hawk.job_manager import AIHawkJobManager
from selenium.common.exceptions import NoSuchElementException
from loguru import logger
from src.logging import logger


@pytest.fixture
Expand Down
2 changes: 1 addition & 1 deletion tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
Loading