|
| 1 | +"""Example of how to configure logging for SQLSpec. |
| 2 | +
|
| 3 | +Since SQLSpec no longer provides a configure_logging function, |
| 4 | +users can set up their own logging configuration as needed. |
| 5 | +""" |
| 6 | + |
| 7 | +import logging |
| 8 | +import sys |
| 9 | + |
| 10 | +from sqlspec.utils.correlation import correlation_context |
| 11 | +from sqlspec.utils.logging import StructuredFormatter, get_logger |
| 12 | + |
| 13 | +__all__ = ("demo_correlation_ids", "setup_advanced_logging", "setup_simple_logging", "setup_structured_logging") |
| 14 | + |
| 15 | + |
| 16 | +# Example 1: Basic logging setup with structured JSON output |
| 17 | +def setup_structured_logging() -> None: |
| 18 | + """Set up structured JSON logging for SQLSpec.""" |
| 19 | + # Get the SQLSpec logger |
| 20 | + sqlspec_logger = logging.getLogger("sqlspec") |
| 21 | + |
| 22 | + # Set the logging level |
| 23 | + sqlspec_logger.setLevel(logging.INFO) |
| 24 | + |
| 25 | + # Create a console handler with structured formatter |
| 26 | + console_handler = logging.StreamHandler(sys.stdout) |
| 27 | + console_handler.setFormatter(StructuredFormatter()) |
| 28 | + |
| 29 | + # Add the handler to the logger |
| 30 | + sqlspec_logger.addHandler(console_handler) |
| 31 | + |
| 32 | + # Don't propagate to the root logger |
| 33 | + sqlspec_logger.propagate = False |
| 34 | + |
| 35 | + print("Structured logging configured for SQLSpec") |
| 36 | + |
| 37 | + |
| 38 | +# Example 2: Simple text logging |
| 39 | +def setup_simple_logging() -> None: |
| 40 | + """Set up simple text logging for SQLSpec.""" |
| 41 | + # Configure basic logging for the entire application |
| 42 | + logging.basicConfig( |
| 43 | + level=logging.INFO, |
| 44 | + format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", |
| 45 | + handlers=[logging.StreamHandler(sys.stdout)], |
| 46 | + ) |
| 47 | + |
| 48 | + print("Simple logging configured") |
| 49 | + |
| 50 | + |
| 51 | +# Example 3: Advanced setup with file output and custom formatting |
| 52 | +def setup_advanced_logging() -> None: |
| 53 | + """Set up advanced logging with multiple handlers.""" |
| 54 | + sqlspec_logger = logging.getLogger("sqlspec") |
| 55 | + sqlspec_logger.setLevel(logging.DEBUG) |
| 56 | + |
| 57 | + # Console handler with simple format |
| 58 | + console_handler = logging.StreamHandler(sys.stdout) |
| 59 | + console_formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s") |
| 60 | + console_handler.setFormatter(console_formatter) |
| 61 | + console_handler.setLevel(logging.INFO) # Only INFO and above to console |
| 62 | + |
| 63 | + # File handler with structured format |
| 64 | + file_handler = logging.FileHandler("sqlspec.log") |
| 65 | + file_handler.setFormatter(StructuredFormatter()) |
| 66 | + file_handler.setLevel(logging.DEBUG) # All messages to file |
| 67 | + |
| 68 | + # Add both handlers |
| 69 | + sqlspec_logger.addHandler(console_handler) |
| 70 | + sqlspec_logger.addHandler(file_handler) |
| 71 | + |
| 72 | + # Don't propagate to avoid duplicate logs |
| 73 | + sqlspec_logger.propagate = False |
| 74 | + |
| 75 | + print("Advanced logging configured with console and file output") |
| 76 | + |
| 77 | + |
| 78 | +# Example 4: Using correlation IDs |
| 79 | +def demo_correlation_ids() -> None: |
| 80 | + """Demonstrate using correlation IDs with logging.""" |
| 81 | + |
| 82 | + logger = get_logger("example") |
| 83 | + |
| 84 | + # Without correlation ID |
| 85 | + logger.info("This log has no correlation ID") |
| 86 | + |
| 87 | + # With correlation ID |
| 88 | + with correlation_context() as correlation_id: |
| 89 | + logger.info("Starting operation with correlation ID: %s", correlation_id) |
| 90 | + logger.info("This log will include the correlation ID automatically") |
| 91 | + |
| 92 | + # Simulate some work |
| 93 | + logger.debug("Processing data...") |
| 94 | + logger.info("Operation completed") |
| 95 | + |
| 96 | + |
| 97 | +if __name__ == "__main__": |
| 98 | + # Choose your logging setup |
| 99 | + print("=== Structured Logging Example ===") |
| 100 | + setup_structured_logging() |
| 101 | + demo_correlation_ids() |
| 102 | + |
| 103 | + print("\n=== Simple Logging Example ===") |
| 104 | + setup_simple_logging() |
| 105 | + |
| 106 | + print("\n=== Advanced Logging Example ===") |
| 107 | + setup_advanced_logging() |
0 commit comments