Spaces:
Sleeping
Sleeping
File size: 3,247 Bytes
8d4382d | 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 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 | 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() |