""" Example: Using the Logging System This example demonstrates how to use the structured logging system in the RAG System application. """ import sys from pathlib import Path # Add src to path for imports sys.path.insert(0, str(Path(__file__).parent.parent / "src")) from config import ( configure_logging, configure_development_logging, configure_production_logging, get_logger, get_settings, ) def example_basic_logging(): """Example 1: Basic logging usage.""" print("\n=== Example 1: Basic Logging ===\n") # Configure logging configure_logging( log_level="INFO", log_to_console=True, json_format=True, ) # Get a logger logger = get_logger(__name__) # Log different types of messages logger.info("application_started", version="1.0.0", environment="development") logger.info("user_action", action="login", user_id="user_123") logger.warning("rate_limit_warning", user_id="user_123", requests=95, limit=100) logger.error("operation_failed", operation="save_document", error="Database timeout") def example_context_binding(): """Example 2: Context binding for request handling.""" print("\n=== Example 2: Context Binding ===\n") configure_logging(log_level="INFO", log_to_console=True, json_format=True) logger = get_logger(__name__) # Simulate handling a request request_id = "req_12345" user_id = "user_789" # Bind context to logger request_logger = logger.bind(request_id=request_id, user_id=user_id) # All subsequent logs will include request_id and user_id request_logger.info("request_received", method="POST", path="/api/documents") request_logger.info("validating_input", fields=["content", "metadata"]) request_logger.info("saving_document", document_id="doc_456") request_logger.info("request_completed", status_code=201, duration_ms=45) def example_exception_logging(): """Example 3: Logging exceptions with stack traces.""" print("\n=== Example 3: Exception Logging ===\n") configure_logging(log_level="ERROR", log_to_console=True, json_format=True) logger = get_logger(__name__) try: # Simulate an error document_id = "doc_123" result = process_document(document_id) # This will raise an error except Exception as e: logger.error( "document_processing_failed", document_id=document_id, error=str(e), error_type=type(e).__name__, exc_info=True, # Include stack trace ) def process_document(document_id: str): """Simulate document processing that fails.""" raise ValueError(f"Invalid document format for {document_id}") def example_development_config(): """Example 4: Development configuration.""" print("\n=== Example 4: Development Configuration ===\n") # Use development configuration (human-readable format) configure_development_logging() logger = get_logger(__name__) logger.debug("debug_info", variable="value", state="initialized") logger.info("processing_started", task="data_import") logger.info("processing_completed", records_processed=1000) def example_production_config(): """Example 5: Production configuration.""" print("\n=== Example 5: Production Configuration ===\n") # Use production configuration (file logging with JSON format) configure_production_logging( log_level="INFO", log_file="logs/example_app.log", ) logger = get_logger(__name__) logger.info("application_started", version="1.0.0", environment="production") logger.info("database_connected", host="localhost", database="rag_system") logger.info("server_listening", host="0.0.0.0", port=8000) print("Logs written to: logs/example_app.log") def example_with_settings(): """Example 6: Using logging with application settings.""" print("\n=== Example 6: Integration with Settings ===\n") # Get application settings settings = get_settings() # Configure logging based on environment if settings.environment == "production": configure_production_logging( log_level=settings.api.log_level, log_file="logs/app.log", ) else: configure_development_logging( log_level=settings.api.log_level, ) logger = get_logger(__name__) logger.info( "application_configured", environment=settings.environment, vector_db_type=settings.vector_db_type, log_level=settings.api.log_level, ) def example_structured_fields(): """Example 7: Using structured fields effectively.""" print("\n=== Example 7: Structured Fields ===\n") configure_logging(log_level="INFO", log_to_console=True, json_format=True) logger = get_logger(__name__) # Good: Use structured fields logger.info( "search_completed", query="machine learning", results_count=42, duration_ms=123, filters={"category": "AI", "year": 2024}, top_score=0.95, ) # Good: Log business events logger.info( "document_indexed", document_id="doc_789", title="Introduction to RAG", size_bytes=15360, chunks_count=12, embedding_model="text-embedding-3-small", ) # Good: Log performance metrics logger.info( "batch_processing_completed", batch_id="batch_001", documents_processed=100, successful=98, failed=2, total_duration_ms=5432, avg_duration_ms=54.32, ) if __name__ == "__main__": print("=" * 70) print("Logging System Examples") print("=" * 70) try: example_basic_logging() example_context_binding() example_exception_logging() example_development_config() example_production_config() example_with_settings() example_structured_fields() print("\n" + "=" * 70) print("All examples completed successfully!") print("=" * 70) except Exception as e: print(f"\nExample failed with error: {e}") import traceback traceback.print_exc() sys.exit(1)