Team Ai
Apppublic

kernelmind/Resume-Screener-API

sourceHugging Faceupdated 6mo agoView on Hugging Face
0likes
logging.py82 linesDownload Raw Back to core
1"""2Logging configuration using loguru.3 4Why loguru instead of stdlib logging?51. Zero-config colored output that actually looks good62. Built-in rotation without handlers/formatters ceremony73. Structured logging (JSON) for production with one line84. Exception tracebacks that show local variables (invaluable for debugging)95. I've wasted too many hours debugging logging.basicConfig issues10"""11 12import sys13from typing import Literal14 15from loguru import logger16 17# Remove default handler - we'll configure our own18logger.remove()19 20 21def setup_logging(22    level: Literal["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"] = "INFO",23    json_logs: bool = False,24) -> None:25    """26    Configure loguru for the application.27    28    Args:29        level: Minimum log level to display30        json_logs: If True, output structured JSON (useful for log aggregators)31    """32    # Ensure Windows consoles can handle unicode log lines gracefully.33    if hasattr(sys.stdout, "reconfigure"):34        sys.stdout.reconfigure(encoding="utf-8", errors="replace")35 36    # Development format - human readable with colors37    dev_format = (38        "<green>{time:YYYY-MM-DD HH:mm:ss}</green> | "39        "<level>{level: <8}</level> | "40        "<cyan>{name}</cyan>:<cyan>{function}</cyan>:<cyan>{line}</cyan> | "41        "<level>{message}</level>"42    )43    44    if json_logs:45        # Production format - structured JSON for log aggregators like Loki/ELK46        logger.add(47            sys.stdout,48            level=level,49            serialize=True,  # This is the magic - JSON output50            backtrace=True,51            diagnose=False,  # Don't leak variable values in production52        )53    else:54        # Development format - colored and human-friendly55        logger.add(56            sys.stdout,57            level=level,58            format=dev_format,59            colorize=True,60            backtrace=True,61            diagnose=True,  # Show variable values in tracebacks (dev only!)62        )63    64    # Also log to file with rotation - because grep is still useful65    logger.add(66        "logs/app.log",67        level=level,68        format="{time:YYYY-MM-DD HH:mm:ss} | {level: <8} | {name}:{function}:{line} | {message}",69        rotation="10 MB",  # Rotate when file hits 10MB70        retention="7 days",  # Keep logs for a week71        compression="gz",  # Compress old logs72        backtrace=True,73        diagnose=False,  # Don't leak secrets in log files74    )75    76    logger.info(f"Logging configured at {level} level")77 78 79# Re-export logger for convenience80# Usage: from src.core.logging import logger81__all__ = ["logger", "setup_logging"]82