import json import datetime import os import traceback from typing import Any, Dict, List, Optional, Union class LLMLogger: def __init__(self, log_file="llm_debug.txt", detailed_log_file="llm_detailed_log.jsonl"): self.log_file = log_file self.detailed_log_file = detailed_log_file self.ensure_log_files() def ensure_log_files(self): """Create log files if they don't exist.""" if not os.path.exists(self.log_file): with open(self.log_file, 'w', encoding='utf-8') as f: f.write(f"LLM Debug Log - Started: {datetime.datetime.now()}\n") f.write("="*80 + "\n\n") # Create JSONL file if it doesn't exist if not os.path.exists(self.detailed_log_file): with open(self.detailed_log_file, 'w', encoding='utf-8') as f: f.write("") # Create empty file def log_call(self, messages: List[Dict], response: Optional[Union[str, Dict]] = None, model: Optional[str] = None, **kwargs): """Log an LLM call with all details.""" timestamp = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") # Log to human-readable text file log_entry = f""" [{timestamp}] LLM Call {"="*50} Model: {model or 'Unknown'} Messages: {json.dumps(messages, indent=2, ensure_ascii=False)} Additional Parameters: {json.dumps(kwargs, indent=2, ensure_ascii=False)} Response: {response if isinstance(response, str) else json.dumps(response, indent=2, ensure_ascii=False) if response else 'No response logged'} {"="*80} """ with open(self.log_file, 'a', encoding='utf-8') as f: f.write(log_entry) # Log to structured JSONL file for programmatic access detailed_entry = { "timestamp": timestamp, "model": model, "messages": messages, "response": response, "metadata": kwargs } with open(self.detailed_log_file, 'a', encoding='utf-8') as f: f.write(json.dumps(detailed_entry, ensure_ascii=False) + "\n") def log_error(self, error: Exception, context: str = ""): """Log an error with full stack trace.""" timestamp = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") stack_trace = traceback.format_exc() log_entry = f""" [{timestamp}] ERROR {"="*50} Context: {context} Error: {str(error)} Type: {type(error).__name__} Stack Trace: {stack_trace} {"="*80} """ with open(self.log_file, 'a', encoding='utf-8') as f: f.write(log_entry) # Also log to structured format detailed_entry = { "timestamp": timestamp, "type": "error", "context": context, "error": str(error), "error_type": type(error).__name__, "stack_trace": stack_trace } with open(self.detailed_log_file, 'a', encoding='utf-8') as f: f.write(json.dumps(detailed_entry, ensure_ascii=False) + "\n") # Global logger instance llm_logger = LLMLogger()