Skip to content

Commit

Permalink
Merge pull request feder-cr#688 from surapuramakhil/ft_logging_enhanc…
Browse files Browse the repository at this point in the history
…ements
  • Loading branch information
feder-cr authored Nov 2, 2024
2 parents 2f25118 + 834f9b5 commit afb869e
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 66 deletions.
18 changes: 7 additions & 11 deletions app_config.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
# In this file, you can set the configurations of the app.

"""
MINIMUM_LOG_LEVEL can only be one of the followings:
- "DEBUG"
- "INFO"
- "WARNING"
- "ERROR"
- "CRITICAL"
"""
MINIMUM_LOG_LEVEL = "DEBUG"
LOG_TO_FILE = True
LOG_TO_CONSOLE = True
from constants import DEBUG, LOG_TO_CONSOLE, LOG_TO_FILE, MINIMUM_LOG_LEVEL

LOG_CONFIG = {
MINIMUM_LOG_LEVEL: DEBUG,
LOG_TO_FILE: True,
LOG_TO_CONSOLE: True
}

MINIMUM_WAIT_TIME = 60
9 changes: 9 additions & 0 deletions constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
DEBUG = "DEBUG"
INFO = "INFO"
WARNING = "WARNING"
ERROR = "ERROR"
CRITICAL = "CRITICAL"

MINIMUM_LOG_LEVEL = "MINIMUM_LOG_LEVEL"
LOG_TO_FILE = "LOG_TO_FILE"
LOG_TO_CONSOLE = "LOG_TO_CONSOLE"
4 changes: 2 additions & 2 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,10 +189,10 @@ def create_and_run_bot(parameters, llm_api_key):
bot.set_parameters(parameters)
bot.start_login()
if (parameters['collectMode'] == True):
print('Collecting')
logger.info('Collecting')
bot.start_collect_data()
else:
print('Applying')
logger.info('Applying')
bot.start_apply()
except WebDriverException as e:
logger.error(f"WebDriver error occurred: {e}")
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 @@ -462,7 +462,7 @@ def extract_job_information_from_tile(self, job_tile):
logger.debug("Extracting job information from tile")
job_title, company, job_location, apply_method, link = "", "", "", "", ""
try:
print(job_tile.get_attribute('outerHTML'))
logger.trace(job_tile.get_attribute('outerHTML'))
job_title = job_tile.find_element(By.CLASS_NAME, 'job-card-list__title').find_element(By.TAG_NAME, 'strong').text

link = job_tile.find_element(By.CLASS_NAME, 'job-card-list__title').get_attribute('href').split('?')[0]
Expand Down
4 changes: 2 additions & 2 deletions src/ai_hawk/linkedIn_easy_applier.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,8 +232,8 @@ def _find_easy_apply_button(self, job: Any) -> WebElement:
time.sleep(random.randint(3, 5))
attempt += 1

page_source = self.driver.page_source
logger.error(f"No clickable 'Easy Apply' button found after 2 attempts. Page source:\n{page_source}")
page_url = self.driver.current_url
logger.error(f"No clickable 'Easy Apply' button found after 2 attempts. page url: {page_url}")
raise Exception("No clickable 'Easy Apply' button found")

def _get_job_description(self) -> str:
Expand Down
3 changes: 1 addition & 2 deletions src/ai_hawk/llm/llm_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,7 @@ def __init__(self, api_key: str, llm_model: str):

def invoke(self, prompt: str) -> BaseMessage:
response = self.chatmodel.invoke(prompt)
logger.debug("Invoking Model from Hugging Face API")
print(response,type(response))
logger.debug(f"Invoking Model from Hugging Face API. Response: {response}, Type: {type(response)}")
return response

class AIAdapter:
Expand Down
50 changes: 25 additions & 25 deletions src/logging.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import atexit
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 app_config import LOG_CONFIG

from selenium.webdriver.remote.remote_connection import LOGGER as selenium_logger
selenium_logger.setLevel(MINIMUM_LOG_LEVEL)

from constants import LOG_TO_CONSOLE, LOG_TO_FILE, MINIMUM_LOG_LEVEL
selenium_logger.setLevel(LOG_CONFIG[MINIMUM_LOG_LEVEL])

log_file = "log/app.log"

Expand All @@ -17,30 +20,27 @@
# 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>"
})
if LOG_CONFIG[LOG_TO_FILE] :
logger.add(
log_file,
level=LOG_CONFIG[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>",
backtrace=True,
diagnose=True
)

# 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)
if LOG_CONFIG[LOG_TO_CONSOLE]:
logger.add(
sys.stderr,
level=LOG_CONFIG[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>",
backtrace=True,
diagnose=True
)


12 changes: 1 addition & 11 deletions src/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,14 +140,4 @@ 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"
logger.debug("Printing text in yellow: %s", text)
print(f"{yellow}{text}{reset}")

13 changes: 1 addition & 12 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import time
from unittest import mock
from selenium.webdriver.remote.webelement import WebElement
from src.utils import ensure_chrome_profile, is_scrollable, scroll_slow, chrome_browser_options, printred, printyellow
from src.utils import ensure_chrome_profile, is_scrollable, scroll_slow, chrome_browser_options

# Mocking logging to avoid actual file writing
@pytest.fixture(autouse=True)
Expand Down Expand Up @@ -83,14 +83,3 @@ def test_chrome_browser_options(mocker):
# Ensure options were set
assert mock_options.add_argument.called
assert options == mock_options

# Test printred and printyellow functions
def test_printred(mocker):
mocker.patch("builtins.print")
printred("Test")
print.assert_called_once_with("\033[91mTest\033[0m")

def test_printyellow(mocker):
mocker.patch("builtins.print")
printyellow("Test")
print.assert_called_once_with("\033[93mTest\033[0m")

0 comments on commit afb869e

Please sign in to comment.