#!/usr/bin/env python3 """Manual test script to verify logging configuration.""" import logging import tempfile from pathlib import Path from claude_coordinator.logging_config import ( setup_logging, format_error_for_discord, log_and_format_error, ErrorTracker ) def test_basic_logging(): """Test basic logging setup and output.""" print("Testing basic logging setup...") with tempfile.TemporaryDirectory() as tmpdir: log_file = Path(tmpdir) / "test.log" # Setup logging setup_logging( log_level="DEBUG", log_file=str(log_file), json_logs=True ) # Create logger and log some messages logger = logging.getLogger(__name__) logger.debug("Debug message", extra={'test_field': 'debug_value'}) logger.info("Info message", extra={'channel_id': '12345'}) logger.warning("Warning message") logger.error("Error message", extra={'error_code': 500}) # Try exception logging try: raise ValueError("Test exception") except ValueError as e: logger.exception("Exception occurred", extra={'context': 'test'}) # Read log file and verify JSON format log_content = log_file.read_text() print("\n--- Log File Contents ---") print(log_content) print("--- End Log Contents ---\n") # Verify it's JSON import json for line in log_content.strip().split('\n'): try: parsed = json.loads(line) assert 'timestamp' in parsed assert 'level' in parsed assert 'message' in parsed print(f"✓ Valid JSON log entry: {parsed['level']} - {parsed['message']}") except json.JSONDecodeError as e: print(f"✗ Invalid JSON: {line[:100]}") raise def test_error_tracking(): """Test error tracking and Discord message formatting.""" print("\nTesting error tracking...") logger = logging.getLogger('error_test') # Test error ID generation error_id1 = ErrorTracker.generate_error_id() error_id2 = ErrorTracker.generate_error_id() assert error_id1 != error_id2 print(f"✓ Generated unique error IDs: {error_id1}, {error_id2}") # Test Discord error formatting msg = format_error_for_discord( error_type="timeout", error_id="abc12345" ) assert "⏱️" in msg assert "abc12345" in msg print(f"✓ Formatted Discord error message:\n{msg}\n") # Test combined logging and formatting error_id, discord_msg = log_and_format_error( logger, error_type="claude_error", message="Test error message", channel_id="67890", session_id="test-session" ) assert len(error_id) == 8 assert error_id in discord_msg print(f"✓ Combined log and format with error_id: {error_id}") def test_log_rotation(): """Test log rotation configuration.""" print("\nTesting log rotation configuration...") with tempfile.TemporaryDirectory() as tmpdir: log_file = Path(tmpdir) / "rotate_test.log" # Setup with small file size for testing setup_logging( log_level="INFO", log_file=str(log_file), json_logs=True, max_bytes=1000, # Small size to trigger rotation backup_count=3 ) logger = logging.getLogger('rotation_test') # Write many log entries to trigger rotation for i in range(100): logger.info(f"Log entry {i} with some padding to increase size" * 5) # Check if rotation occurred log_files = list(Path(tmpdir).glob("rotate_test.log*")) print(f"✓ Log rotation: {len(log_files)} files created") for lf in log_files: print(f" - {lf.name} ({lf.stat().st_size} bytes)") if __name__ == "__main__": print("=== Manual Logging Tests ===\n") try: test_basic_logging() test_error_tracking() test_log_rotation() print("\n=== All Manual Tests Passed ===") except Exception as e: print(f"\n✗ Test failed: {e}") import traceback traceback.print_exc() exit(1)