import sys
from loguru import logger
import time
from pathlib import Path
def setup_api_logging(log_path: Path ):
"""
Configure Loguru logger for the API
Args:
log_path: Optional path to store log files
"""
# Remove default handler
logger.remove()
# Add stdout handler with custom format
logger.add(
sys.stdout,
colorize=True,
format="{time:YYYY-MM-DD HH:mm:ss} | {level: <8} | {name}:{function}:{line} - {message}",
level="INFO"
)
# Add file logging if path provided
if log_path:
log_path.parent.mkdir(parents=True, exist_ok=True)
logger.add(
log_path / f"api_{time.strftime('%Y%m%d')}.log",
rotation="00:00", # New file at midnight
compression="zip", # Compress old logs
retention="30 days", # Keep logs for 30 days
format="{time:YYYY-MM-DD HH:mm:ss} | {level: <8} | {name}:{function}:{line} - {message}",
level="DEBUG"
)
logger.info("API logging configured")
return logger