"""Structured logging setup using Rich."""
|
|
import logging
|
from pathlib import Path
|
|
from rich.console import Console
|
from rich.logging import RichHandler
|
|
console = Console()
|
|
|
def setup_logging(
|
level: str = "INFO",
|
log_file: Path | None = None,
|
) -> logging.Logger:
|
"""Configure logging with Rich handler for pretty console output.
|
|
Args:
|
level: Logging level (DEBUG, INFO, WARNING, ERROR, CRITICAL)
|
log_file: Optional path to log file
|
|
Returns:
|
Configured logger instance
|
"""
|
handlers: list[logging.Handler] = [
|
RichHandler(
|
console=console,
|
rich_tracebacks=True,
|
tracebacks_show_locals=True,
|
show_time=True,
|
show_path=False,
|
)
|
]
|
|
if log_file:
|
log_file.parent.mkdir(parents=True, exist_ok=True)
|
file_handler = logging.FileHandler(log_file)
|
file_handler.setFormatter(
|
logging.Formatter("%(asctime)s | %(levelname)-8s | %(name)s | %(message)s")
|
)
|
handlers.append(file_handler)
|
|
logging.basicConfig(
|
level=getattr(logging, level.upper()),
|
format="%(message)s",
|
datefmt="[%X]",
|
handlers=handlers,
|
)
|
|
return logging.getLogger("archbuild")
|
|
|
def get_logger(name: str) -> logging.Logger:
|
"""Get a child logger for a specific module.
|
|
Args:
|
name: Module name for the logger
|
|
Returns:
|
Logger instance
|
"""
|
return logging.getLogger(f"archbuild.{name}")
|