Spaces:
Paused
Paused
| import logging | |
| import sys | |
| from datetime import datetime | |
| from pathlib import Path | |
| from logging.handlers import RotatingFileHandler | |
| from ..core.config import settings | |
| # Create logs directory if it doesn't exist | |
| logs_dir = Path("logs") | |
| logs_dir.mkdir(exist_ok=True) | |
| # Configure logging format | |
| log_format = logging.Formatter( | |
| "%(asctime)s - %(name)s - %(levelname)s - %(message)s" | |
| ) | |
| def setup_logger(name: str) -> logging.Logger: | |
| logger = logging.getLogger(name) | |
| logger.setLevel(logging.INFO) | |
| # Console handler | |
| console_handler = logging.StreamHandler(sys.stdout) | |
| console_handler.setFormatter(log_format) | |
| logger.addHandler(console_handler) | |
| # File handler with rotation | |
| file_handler = RotatingFileHandler( | |
| logs_dir / f"{name}.log", | |
| maxBytes=10485760, # 10MB | |
| backupCount=5 | |
| ) | |
| file_handler.setFormatter(log_format) | |
| logger.addHandler(file_handler) | |
| return logger | |
| # Create main application logger | |
| logger = setup_logger("admin_dashboard") | |
| def log_api_request(method: str, path: str, status_code: int, duration: float): | |
| """Log API request details""" | |
| logger.info( | |
| f"API Request - Method: {method}, Path: {path}, " | |
| f"Status: {status_code}, Duration: {duration:.3f}s" | |
| ) | |
| def log_error(error: Exception, context: dict = None): | |
| """Log error with context""" | |
| logger.error( | |
| f"Error: {str(error)}, Type: {type(error).__name__}, " | |
| f"Context: {context or {}}" | |
| ) | |
| def log_database_operation(operation: str, collection: str, success: bool): | |
| """Log database operations""" | |
| logger.info( | |
| f"Database Operation - Type: {operation}, Collection: {collection}, " | |
| f"Success: {success}" | |
| ) |