UAP-Data-Analysis-Tool / utils /logger_config.py
Ashoka74's picture
Deploy current work to HF Space (slim)
a1aef88
Raw
History Blame Contribute Delete
2.89 kB
"""
Logging configuration for UAP Data Analysis Tool
Provides consistent logging setup across all modules
"""
import logging
import sys
from datetime import datetime
import os
def setup_logging(
log_level: str = "INFO",
log_file: str = None,
log_format: str = None
) -> logging.Logger:
"""
Set up logging configuration for the application
Args:
log_level: Logging level (DEBUG, INFO, WARNING, ERROR, CRITICAL)
log_file: Optional log file path
log_format: Custom log format string
Returns:
Configured logger instance
"""
# Default format if not provided
if log_format is None:
log_format = '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
# Create logs directory if logging to file
if log_file:
os.makedirs(os.path.dirname(log_file), exist_ok=True)
# Configure root logger
logging.basicConfig(
level=getattr(logging, log_level.upper()),
format=log_format,
handlers=[
logging.StreamHandler(sys.stdout),
logging.FileHandler(log_file) if log_file else logging.NullHandler()
]
)
# Set specific loggers to WARNING to reduce noise
logging.getLogger('matplotlib').setLevel(logging.WARNING)
logging.getLogger('PIL').setLevel(logging.WARNING)
logging.getLogger('urllib3').setLevel(logging.WARNING)
logger = logging.getLogger('UAP_Analytics')
logger.info(f"Logging initialized at {log_level} level")
return logger
# Performance logging decorator
def log_performance(func):
"""Decorator to log function execution time"""
import functools
import time
@functools.wraps(func)
def wrapper(*args, **kwargs):
logger = logging.getLogger(func.__module__)
start_time = time.time()
try:
result = func(*args, **kwargs)
execution_time = time.time() - start_time
logger.info(f"{func.__name__} completed in {execution_time:.2f}s")
return result
except Exception as e:
execution_time = time.time() - start_time
logger.error(f"{func.__name__} failed after {execution_time:.2f}s: {e}")
raise
return wrapper
# Error logging decorator
def log_errors(func):
"""Decorator to log function errors"""
import functools
@functools.wraps(func)
def wrapper(*args, **kwargs):
logger = logging.getLogger(func.__module__)
try:
return func(*args, **kwargs)
except Exception as e:
logger.error(f"Error in {func.__name__}: {e}", exc_info=True)
raise
return wrapper
# Initialize default logger
default_logger = setup_logging(
log_level=os.getenv('LOG_LEVEL', 'INFO'),
log_file=os.getenv('LOG_FILE', None)
)