Initial commit — PlantGuideScraper project

This commit is contained in:
Trey T
2026-04-12 09:54:27 -05:00
commit 6926f502c5
87 changed files with 29120 additions and 0 deletions

View File

@@ -0,0 +1,92 @@
import logging
import os
from datetime import datetime
from pathlib import Path
from app.config import get_settings
settings = get_settings()
def setup_logging():
"""Configure file and console logging."""
logs_path = Path(settings.logs_path)
logs_path.mkdir(parents=True, exist_ok=True)
# Create a dated log file
log_file = logs_path / f"scraper_{datetime.now().strftime('%Y-%m-%d')}.log"
# Configure root logger
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
handlers=[
logging.FileHandler(log_file),
logging.StreamHandler()
]
)
return logging.getLogger("plant_scraper")
def get_logger(name: str = "plant_scraper"):
"""Get a logger instance."""
logs_path = Path(settings.logs_path)
logs_path.mkdir(parents=True, exist_ok=True)
logger = logging.getLogger(name)
if not logger.handlers:
logger.setLevel(logging.INFO)
# File handler with daily rotation
log_file = logs_path / f"scraper_{datetime.now().strftime('%Y-%m-%d')}.log"
file_handler = logging.FileHandler(log_file)
file_handler.setLevel(logging.INFO)
file_handler.setFormatter(logging.Formatter(
'%(asctime)s - %(name)s - %(levelname)s - %(message)s'
))
# Console handler
console_handler = logging.StreamHandler()
console_handler.setLevel(logging.INFO)
console_handler.setFormatter(logging.Formatter(
'%(asctime)s - %(levelname)s - %(message)s'
))
logger.addHandler(file_handler)
logger.addHandler(console_handler)
return logger
def get_job_logger(job_id: int):
"""Get a logger specific to a job, writing to a job-specific file."""
logs_path = Path(settings.logs_path)
logs_path.mkdir(parents=True, exist_ok=True)
logger = logging.getLogger(f"job_{job_id}")
if not logger.handlers:
logger.setLevel(logging.DEBUG)
# Job-specific log file
job_log_file = logs_path / f"job_{job_id}.log"
file_handler = logging.FileHandler(job_log_file)
file_handler.setLevel(logging.DEBUG)
file_handler.setFormatter(logging.Formatter(
'%(asctime)s - %(levelname)s - %(message)s'
))
# Also log to daily file
daily_log_file = logs_path / f"scraper_{datetime.now().strftime('%Y-%m-%d')}.log"
daily_handler = logging.FileHandler(daily_log_file)
daily_handler.setLevel(logging.INFO)
daily_handler.setFormatter(logging.Formatter(
'%(asctime)s - job_%(name)s - %(levelname)s - %(message)s'
))
logger.addHandler(file_handler)
logger.addHandler(daily_handler)
return logger