chatbot / logging_config.py
subramaniansrc's picture
Create logging_config.py
0f5d75b verified
Raw
History Blame Contribute Delete
1.22 kB
"""
Structured logging configuration for the RAG chatbot.
Outputs both to console and a rotating file handler.
"""
import logging
import os
from logging.handlers import RotatingFileHandler
from pathlib import Path
def setup_logging(
log_dir: str = "logs",
log_file: str = "admissions_rag.log",
level: int = logging.INFO,
max_bytes: int = 5 * 1024 * 1024, # 5 MB
backup_count: int = 3,
) -> logging.Logger:
Path(log_dir).mkdir(parents=True, exist_ok=True)
log_path = os.path.join(log_dir, log_file)
fmt = logging.Formatter(
fmt="%(asctime)s | %(levelname)-8s | %(name)s | %(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
)
root_logger = logging.getLogger()
root_logger.setLevel(level)
# Console handler
if not any(isinstance(h, logging.StreamHandler) for h in root_logger.handlers):
ch = logging.StreamHandler()
ch.setFormatter(fmt)
root_logger.addHandler(ch)
# File handler
fh = RotatingFileHandler(log_path, maxBytes=max_bytes, backupCount=backup_count)
fh.setFormatter(fmt)
root_logger.addHandler(fh)
return root_logger
def get_logger(name: str) -> logging.Logger:
return logging.getLogger(name)