Spaces:
Sleeping

Digitalytics / src /utils /logging_config.py
faseehsandhu's picture
added new module of area insights
c088262
Raw History Blame Contribute Delete
2.03 kB
"""
Centralized logging configuration for ML Column Recommender.
This module sets up consistent logging across all application services
with file rotation, console output, and structured formatting.
"""
import logging
import logging.handlers
from pathlib import Path
def setup_logging(log_level=logging.INFO):
"""
Configure centralized logging for the entire application.
Args:
log_level: Logging level (default: logging.INFO)
Returns:
Root logger instance
"""
# Create logs directory
log_dir = Path(".logs")
log_dir.mkdir(exist_ok=True)
# Define consistent format
format_str = (
"%(asctime)s | %(levelname)-8s | %(name)-20s | %(funcName)s:%(lineno)d | %(message)s"
)
formatter = logging.Formatter(format_str, datefmt="%Y-%m-%d %H:%M:%S")
# Get root logger
root = logging.getLogger()
root.setLevel(log_level)
# Remove any existing handlers
for handler in root.handlers[:]:
root.removeHandler(handler)
# File handler with rotation (10MB per file, keep 5 backups)
fh = logging.handlers.RotatingFileHandler(
log_dir / "app.log",
maxBytes=10_000_000, # 10MB
backupCount=5,
encoding='utf-8' # Handle Unicode characters
)
fh.setLevel(log_level)
fh.setFormatter(formatter)
root.addHandler(fh)
# Console handler (show INFO and above) - use UTF-8 encoding for Windows console
ch = logging.StreamHandler()
ch.setLevel(logging.INFO)
ch.setFormatter(formatter)
ch.stream.reconfigure(encoding='utf-8') # Force UTF-8 for console output
root.addHandler(ch)
# Log startup
root.info("=" * 80)
root.info("Logging initialized successfully")
root.info("=" * 80)
return root
def get_logger(name):
"""
Get a logger instance with the given name.
Args:
name: Module name or logger name
Returns:
Logger instance
"""
return logging.getLogger(name)