""" Manual test script for logging configuration. This script demonstrates the logging system functionality and can be run to verify that logging is working correctly. """ import sys import tempfile from pathlib import Path # Add src to path sys.path.insert(0, str(Path(__file__).parent / "src")) from config.logging import ( configure_logging, configure_development_logging, configure_production_logging, get_logger, ) def test_console_logging(): """Test console logging with JSON format.""" print("\n=== Testing Console Logging (JSON Format) ===") configure_logging( log_level="INFO", log_to_console=True, log_to_file=False, json_format=True, ) logger = get_logger(__name__) logger.info("test_message", test_key="test_value", user_id="user_123") logger.warning("warning_message", warning_type="test") logger.error("error_message", error_code=500) print("✓ Console logging test completed\n") def test_file_logging(): """Test file logging with rotation.""" print("=== Testing File Logging ===") # Create temporary log file with tempfile.NamedTemporaryFile(mode='w', delete=False, suffix='.log') as f: log_file = f.name try: configure_logging( log_level="DEBUG", log_file=log_file, log_to_console=False, log_to_file=True, json_format=True, max_file_size=1024, # Small size for testing backup_count=2, ) logger = get_logger(__name__) logger.debug("debug_message", detail="This is a debug message") logger.info("info_message", detail="This is an info message") logger.warning("warning_message", detail="This is a warning") logger.error("error_message", detail="This is an error") # Read and display log file content log_path = Path(log_file) if log_path.exists(): content = log_path.read_text() print(f"Log file created at: {log_file}") print(f"Log file size: {log_path.stat().st_size} bytes") print(f"Log entries: {len(content.splitlines())}") print("✓ File logging test completed\n") else: print("✗ Log file was not created\n") finally: # Cleanup - close all handlers first import logging for handler in logging.root.handlers[:]: handler.close() logging.root.removeHandler(handler) # Now try to delete the file try: if Path(log_file).exists(): Path(log_file).unlink() except PermissionError: # File is still locked on Windows, that's okay print(f"Note: Could not delete temp log file (file locked): {log_file}") def test_development_logging(): """Test development logging configuration.""" print("=== Testing Development Logging (Human-Readable) ===") configure_development_logging() logger = get_logger(__name__) logger.debug("development_debug", feature="new_feature", status="testing") logger.info("development_info", action="user_login", user="dev_user") print("✓ Development logging test completed\n") def test_context_binding(): """Test logger context binding.""" print("=== Testing Context Binding ===") configure_logging( log_level="INFO", log_to_console=True, json_format=True, ) # Create logger with bound context logger = get_logger(__name__) request_logger = logger.bind(request_id="req_12345", user_id="user_789") request_logger.info("request_started", method="GET", path="/api/documents") request_logger.info("request_processing", step="validation") request_logger.info("request_completed", status_code=200, duration_ms=45) print("✓ Context binding test completed\n") def test_exception_logging(): """Test exception logging with stack traces.""" print("=== Testing Exception Logging ===") configure_logging( log_level="ERROR", log_to_console=True, json_format=True, ) logger = get_logger(__name__) try: # Simulate an error result = 1 / 0 except ZeroDivisionError as e: logger.error( "division_error", error=str(e), operation="divide", exc_info=True, ) print("✓ Exception logging test completed\n") def test_log_levels(): """Test different log levels.""" print("=== Testing Log Levels ===") configure_logging( log_level="DEBUG", log_to_console=True, json_format=False, # Human-readable for this test ) logger = get_logger(__name__) logger.debug("This is a DEBUG message") logger.info("This is an INFO message") logger.warning("This is a WARNING message") logger.error("This is an ERROR message") logger.critical("This is a CRITICAL message") print("✓ Log levels test completed\n") if __name__ == "__main__": print("=" * 60) print("Logging System Manual Tests") print("=" * 60) try: test_console_logging() test_file_logging() test_development_logging() test_context_binding() test_exception_logging() test_log_levels() print("=" * 60) print("All tests completed successfully! ✓") print("=" * 60) except Exception as e: print(f"\n✗ Test failed with error: {e}") import traceback traceback.print_exc() sys.exit(1)