logging_example.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. """
  2. Example: Using the Logging System
  3. This example demonstrates how to use the structured logging system
  4. in the RAG System application.
  5. """
  6. import sys
  7. from pathlib import Path
  8. # Add src to path for imports
  9. sys.path.insert(0, str(Path(__file__).parent.parent / "src"))
  10. from config import (
  11. configure_logging,
  12. configure_development_logging,
  13. configure_production_logging,
  14. get_logger,
  15. get_settings,
  16. )
  17. def example_basic_logging():
  18. """Example 1: Basic logging usage."""
  19. print("\n=== Example 1: Basic Logging ===\n")
  20. # Configure logging
  21. configure_logging(
  22. log_level="INFO",
  23. log_to_console=True,
  24. json_format=True,
  25. )
  26. # Get a logger
  27. logger = get_logger(__name__)
  28. # Log different types of messages
  29. logger.info("application_started", version="1.0.0", environment="development")
  30. logger.info("user_action", action="login", user_id="user_123")
  31. logger.warning("rate_limit_warning", user_id="user_123", requests=95, limit=100)
  32. logger.error("operation_failed", operation="save_document", error="Database timeout")
  33. def example_context_binding():
  34. """Example 2: Context binding for request handling."""
  35. print("\n=== Example 2: Context Binding ===\n")
  36. configure_logging(log_level="INFO", log_to_console=True, json_format=True)
  37. logger = get_logger(__name__)
  38. # Simulate handling a request
  39. request_id = "req_12345"
  40. user_id = "user_789"
  41. # Bind context to logger
  42. request_logger = logger.bind(request_id=request_id, user_id=user_id)
  43. # All subsequent logs will include request_id and user_id
  44. request_logger.info("request_received", method="POST", path="/api/documents")
  45. request_logger.info("validating_input", fields=["content", "metadata"])
  46. request_logger.info("saving_document", document_id="doc_456")
  47. request_logger.info("request_completed", status_code=201, duration_ms=45)
  48. def example_exception_logging():
  49. """Example 3: Logging exceptions with stack traces."""
  50. print("\n=== Example 3: Exception Logging ===\n")
  51. configure_logging(log_level="ERROR", log_to_console=True, json_format=True)
  52. logger = get_logger(__name__)
  53. try:
  54. # Simulate an error
  55. document_id = "doc_123"
  56. result = process_document(document_id) # This will raise an error
  57. except Exception as e:
  58. logger.error(
  59. "document_processing_failed",
  60. document_id=document_id,
  61. error=str(e),
  62. error_type=type(e).__name__,
  63. exc_info=True, # Include stack trace
  64. )
  65. def process_document(document_id: str):
  66. """Simulate document processing that fails."""
  67. raise ValueError(f"Invalid document format for {document_id}")
  68. def example_development_config():
  69. """Example 4: Development configuration."""
  70. print("\n=== Example 4: Development Configuration ===\n")
  71. # Use development configuration (human-readable format)
  72. configure_development_logging()
  73. logger = get_logger(__name__)
  74. logger.debug("debug_info", variable="value", state="initialized")
  75. logger.info("processing_started", task="data_import")
  76. logger.info("processing_completed", records_processed=1000)
  77. def example_production_config():
  78. """Example 5: Production configuration."""
  79. print("\n=== Example 5: Production Configuration ===\n")
  80. # Use production configuration (file logging with JSON format)
  81. configure_production_logging(
  82. log_level="INFO",
  83. log_file="logs/example_app.log",
  84. )
  85. logger = get_logger(__name__)
  86. logger.info("application_started", version="1.0.0", environment="production")
  87. logger.info("database_connected", host="localhost", database="rag_system")
  88. logger.info("server_listening", host="0.0.0.0", port=8000)
  89. print("Logs written to: logs/example_app.log")
  90. def example_with_settings():
  91. """Example 6: Using logging with application settings."""
  92. print("\n=== Example 6: Integration with Settings ===\n")
  93. # Get application settings
  94. settings = get_settings()
  95. # Configure logging based on environment
  96. if settings.environment == "production":
  97. configure_production_logging(
  98. log_level=settings.api.log_level,
  99. log_file="logs/app.log",
  100. )
  101. else:
  102. configure_development_logging(
  103. log_level=settings.api.log_level,
  104. )
  105. logger = get_logger(__name__)
  106. logger.info(
  107. "application_configured",
  108. environment=settings.environment,
  109. vector_db_type=settings.vector_db_type,
  110. log_level=settings.api.log_level,
  111. )
  112. def example_structured_fields():
  113. """Example 7: Using structured fields effectively."""
  114. print("\n=== Example 7: Structured Fields ===\n")
  115. configure_logging(log_level="INFO", log_to_console=True, json_format=True)
  116. logger = get_logger(__name__)
  117. # Good: Use structured fields
  118. logger.info(
  119. "search_completed",
  120. query="machine learning",
  121. results_count=42,
  122. duration_ms=123,
  123. filters={"category": "AI", "year": 2024},
  124. top_score=0.95,
  125. )
  126. # Good: Log business events
  127. logger.info(
  128. "document_indexed",
  129. document_id="doc_789",
  130. title="Introduction to RAG",
  131. size_bytes=15360,
  132. chunks_count=12,
  133. embedding_model="text-embedding-3-small",
  134. )
  135. # Good: Log performance metrics
  136. logger.info(
  137. "batch_processing_completed",
  138. batch_id="batch_001",
  139. documents_processed=100,
  140. successful=98,
  141. failed=2,
  142. total_duration_ms=5432,
  143. avg_duration_ms=54.32,
  144. )
  145. if __name__ == "__main__":
  146. print("=" * 70)
  147. print("Logging System Examples")
  148. print("=" * 70)
  149. try:
  150. example_basic_logging()
  151. example_context_binding()
  152. example_exception_logging()
  153. example_development_config()
  154. example_production_config()
  155. example_with_settings()
  156. example_structured_fields()
  157. print("\n" + "=" * 70)
  158. print("All examples completed successfully!")
  159. print("=" * 70)
  160. except Exception as e:
  161. print(f"\nExample failed with error: {e}")
  162. import traceback
  163. traceback.print_exc()
  164. sys.exit(1)