EduVid-LLM / src /utils /smart_logger.py
gokul00060's picture
Revamped Whole Code Base Architecture
ac02dae
# src/utils/smart_logger.py
import time
from datetime import datetime
from typing import Any, Optional
from rich.console import Console
from src.config import LOG_CONFIG, SUCCESS, FAIL, WARN, INFO
class SmartLogger:
def __init__(self):
self.console = Console()
self.start_times = {}
self.operation_count = {}
def truncate_text(self, text: str, max_length: int = None) -> str:
"""Intelligently truncate text while preserving meaning"""
max_len = max_length or LOG_CONFIG['truncate_threshold']
if len(text) <= max_len:
return text
return text[:max_len-3] + "..."
def format_size(self, size_bytes: int) -> str:
"""Format file sizes in human readable format"""
for unit in ['B', 'KB', 'MB', 'GB']:
if size_bytes < 1024:
return f"{size_bytes:.1f}{unit}"
size_bytes /= 1024
return f"{size_bytes:.1f}TB"
def format_duration(self, seconds: float) -> str:
"""Format duration in human readable format"""
if seconds < 1:
return f"{seconds*1000:.0f}ms"
elif seconds < 60:
return f"{seconds:.1f}s"
else:
mins, secs = divmod(seconds, 60)
return f"{mins:.0f}m{secs:.0f}s"
def start_operation(self, operation_id: str, description: str):
"""Start timing an operation"""
self.start_times[operation_id] = time.time()
short_desc = self.truncate_text(description, 60)
self.console.print(f"{INFO} {short_desc}")
def end_operation(self, operation_id: str, result: str, details: Optional[str] = None):
"""End timing an operation and show result"""
if operation_id in self.start_times:
duration = time.time() - self.start_times[operation_id]
duration_str = self.format_duration(duration)
result_icon = SUCCESS if "success" in result.lower() or "✅" in result else FAIL if "fail" in result.lower() or "❌" in result else WARN
base_msg = f"{result_icon} {self.truncate_text(result, 50)} ({duration_str})"
if details and LOG_CONFIG['show_file_sizes']:
details_short = self.truncate_text(details, 30)
self.console.print(f"{base_msg} | {details_short}")
else:
self.console.print(base_msg)
del self.start_times[operation_id]
else:
self.console.print(f"{WARN} {self.truncate_text(result, 70)}")
def quick_log(self, level: str, message: str, extra: Optional[str] = None):
"""Quick single-line logging"""
icon_map = {"info": INFO, "success": SUCCESS, "error": FAIL, "warn": WARN}
icon = icon_map.get(level.lower(), INFO)
base_msg = self.truncate_text(message, 60)
if extra:
extra_short = self.truncate_text(extra, 20)
self.console.print(f"{icon} {base_msg} | {extra_short}")
else:
self.console.print(f"{icon} {base_msg}")
def progress_update(self, current: int, total: int, operation: str):
"""Show progress in compact format"""
percentage = (current / total) * 100 if total > 0 else 0
op_short = self.truncate_text(operation, 40)
self.console.print(f"{INFO} {op_short} ({current}/{total} - {percentage:.0f}%)")
def error_with_context(self, error: str, context: Optional[str] = None):
"""Log error with minimal context"""
error_short = self.truncate_text(str(error), 50)
if context:
context_short = self.truncate_text(context, 30)
self.console.print(f"{FAIL} {error_short}")
self.console.print(f" Context: {context_short}")
else:
self.console.print(f"{FAIL} {error_short}")
# Global logger instance
logger = SmartLogger()