Skip to content

Commit

Permalink
moves log functions to logging.py
Browse files Browse the repository at this point in the history
- updates tests and code to use src/logging
- exclude log files
  • Loading branch information
sarob committed Oct 27, 2024
1 parent b9bf1db commit c3c9879
Show file tree
Hide file tree
Showing 13 changed files with 63 additions and 57 deletions.
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: 1 addition & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/aihawk_authenticator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion src/aihawk_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/aihawk_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
2 changes: 1 addition & 1 deletion src/aihawk_job_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


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
2 changes: 1 addition & 1 deletion src/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
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)

46 changes: 2 additions & 44 deletions src/utils.py
Original file line number Diff line number Diff line change
@@ -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": "<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)

# 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")

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 src.aihawk_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

0 comments on commit c3c9879

Please sign in to comment.