scrapeRL / backend /app /utils /logging.py
NeerajCodz's picture
feat: add API routes and utility modules
27cde0c
"""Logging utilities for ScrapeRL backend."""
import logging
import sys
from typing import Optional
def setup_logging(
level: str = "INFO",
format_string: Optional[str] = None,
log_file: Optional[str] = None,
) -> None:
"""
Configure logging for the application.
Args:
level: Logging level (DEBUG, INFO, WARNING, ERROR, CRITICAL)
format_string: Custom format string for log messages
log_file: Optional file path to write logs to
"""
if format_string is None:
format_string = "%(asctime)s - %(name)s - %(levelname)s - %(message)s"
log_level = getattr(logging, level.upper(), logging.INFO)
handlers: list[logging.Handler] = [logging.StreamHandler(sys.stdout)]
if log_file:
file_handler = logging.FileHandler(log_file)
handlers.append(file_handler)
logging.basicConfig(
level=log_level,
format=format_string,
handlers=handlers,
)
# Reduce noise from third-party libraries
logging.getLogger("httpx").setLevel(logging.WARNING)
logging.getLogger("httpcore").setLevel(logging.WARNING)
logging.getLogger("urllib3").setLevel(logging.WARNING)
def get_logger(name: str) -> logging.Logger:
"""
Get a logger instance with the specified name.
Args:
name: Logger name, typically __name__ of the calling module
Returns:
Configured logger instance
"""
return logging.getLogger(name)