Spaces:
Build error
Build error
Create logging_config.py
Browse files- logging_config.py +45 -0
logging_config.py
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Structured logging configuration for the RAG chatbot.
|
| 3 |
+
Outputs both to console and a rotating file handler.
|
| 4 |
+
"""
|
| 5 |
+
|
| 6 |
+
import logging
|
| 7 |
+
import os
|
| 8 |
+
from logging.handlers import RotatingFileHandler
|
| 9 |
+
from pathlib import Path
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
def setup_logging(
|
| 13 |
+
log_dir: str = "logs",
|
| 14 |
+
log_file: str = "admissions_rag.log",
|
| 15 |
+
level: int = logging.INFO,
|
| 16 |
+
max_bytes: int = 5 * 1024 * 1024, # 5 MB
|
| 17 |
+
backup_count: int = 3,
|
| 18 |
+
) -> logging.Logger:
|
| 19 |
+
Path(log_dir).mkdir(parents=True, exist_ok=True)
|
| 20 |
+
log_path = os.path.join(log_dir, log_file)
|
| 21 |
+
|
| 22 |
+
fmt = logging.Formatter(
|
| 23 |
+
fmt="%(asctime)s | %(levelname)-8s | %(name)s | %(message)s",
|
| 24 |
+
datefmt="%Y-%m-%d %H:%M:%S",
|
| 25 |
+
)
|
| 26 |
+
|
| 27 |
+
root_logger = logging.getLogger()
|
| 28 |
+
root_logger.setLevel(level)
|
| 29 |
+
|
| 30 |
+
# Console handler
|
| 31 |
+
if not any(isinstance(h, logging.StreamHandler) for h in root_logger.handlers):
|
| 32 |
+
ch = logging.StreamHandler()
|
| 33 |
+
ch.setFormatter(fmt)
|
| 34 |
+
root_logger.addHandler(ch)
|
| 35 |
+
|
| 36 |
+
# File handler
|
| 37 |
+
fh = RotatingFileHandler(log_path, maxBytes=max_bytes, backupCount=backup_count)
|
| 38 |
+
fh.setFormatter(fmt)
|
| 39 |
+
root_logger.addHandler(fh)
|
| 40 |
+
|
| 41 |
+
return root_logger
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
def get_logger(name: str) -> logging.Logger:
|
| 45 |
+
return logging.getLogger(name)
|