Spaces:
Running
Running
#!/usr/bin/env python3 | |
""" | |
Shared logging utilities for GAIA test scripts | |
""" | |
import sys | |
from datetime import datetime | |
from contextlib import contextmanager | |
class TeeOutput: | |
"""Class to write to both console and log file simultaneously""" | |
def __init__(self, log_file): | |
self.log_file = log_file | |
self.terminal = sys.stdout | |
def write(self, message): | |
self.terminal.write(message) | |
self.log_file.write(message) | |
self.log_file.flush() # Ensure immediate write to file | |
def flush(self): | |
self.terminal.flush() | |
self.log_file.flush() | |
def test_logger(test_name: str, question_id: str = None): | |
""" | |
Context manager for test logging that writes to both console and file | |
Args: | |
test_name: Name of the test (e.g., "specific_question", "routing") | |
question_id: Optional question ID for specific question tests | |
Usage: | |
with test_logger("specific_question", "abc123") as log_file: | |
print("This will go to both console and log file") | |
""" | |
# Create timestamped log file | |
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S") | |
if question_id: | |
log_filename = f"logs/test_{test_name}_{question_id[:8]}_{timestamp}.log" | |
log_title = f"GAIA {test_name.title().replace('_', ' ')} Test - Question: {question_id}" | |
else: | |
log_filename = f"logs/test_{test_name}_{timestamp}.log" | |
log_title = f"GAIA {test_name.title().replace('_', ' ')} Test" | |
# Set up logging to both console and file | |
with open(log_filename, 'w') as log_file: | |
# Write header to log file | |
log_file.write(f"{log_title}\n") | |
log_file.write(f"Timestamp: {datetime.now().isoformat()}\n") | |
log_file.write("=" * 60 + "\n\n") | |
# Redirect stdout to both console and log file | |
original_stdout = sys.stdout | |
sys.stdout = TeeOutput(log_file) | |
try: | |
print(f"๐ Logging to: {log_filename}") | |
yield log_filename | |
finally: | |
# Restore original stdout | |
sys.stdout = original_stdout | |
# Final message (only to console) | |
print(f"\n๐ Test completed. Full log saved to: {log_filename}") | |
def create_log_filename(test_name: str, question_id: str = None) -> str: | |
""" | |
Create a standardized log filename | |
Args: | |
test_name: Name of the test | |
question_id: Optional question ID | |
Returns: | |
Formatted log filename with timestamp | |
""" | |
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S") | |
if question_id: | |
return f"logs/test_{test_name}_{question_id[:8]}_{timestamp}.log" | |
else: | |
return f"logs/test_{test_name}_{timestamp}.log" |