darachhat
feat: build production-ready Khmer Document Corpus v0.2.0 with Typer CLI, PyMuPDF, Polars, and DI architecture
c4e128a
Raw
History Blame Contribute Delete
1.89 kB
"""Loguru logging configuration for the KDC project."""
from __future__ import annotations
import sys
from pathlib import Path
from loguru import logger
def setup_logging(
level: str = "INFO",
sink_file: Path | None = None,
rotation: str = "10 MB",
retention: str = "30 days",
*,
colorize: bool = True,
) -> None:
"""
Configure Loguru sinks.
Always adds a stderr sink.
Optionally adds a rotating file sink when sink_file is provided.
"""
# Remove the default Loguru handler
logger.remove()
# ── Console sink ──────────────────────────────────────────────────────────
logger.add(
sys.stderr,
level=level.upper(),
colorize=colorize,
format=(
"<green>{time:YYYY-MM-DD HH:mm:ss}</green> | "
"<level>{level: <8}</level> | "
"<cyan>{name}</cyan>:<cyan>{function}</cyan>:<cyan>{line}</cyan> — "
"<level>{message}</level>"
),
)
# ── File sink ─────────────────────────────────────────────────────────────
if sink_file is not None:
sink_file.parent.mkdir(parents=True, exist_ok=True)
logger.add(
str(sink_file),
level=level.upper(),
rotation=rotation,
retention=retention,
encoding="utf-8",
format=(
"{time:YYYY-MM-DD HH:mm:ss} | {level: <8} | " "{name}:{function}:{line} — {message}"
),
)
def get_logger(name: str) -> logger: # type: ignore[valid-type]
"""Return a bound Loguru logger with a module context."""
return logger.bind(name=name)