Spaces:
Paused
Paused
File size: 1,777 Bytes
a3b84cc |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
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}"
) |