|
12 | 12 |
|
13 | 13 | from sqlspec._serialization import encode_json |
14 | 14 |
|
15 | | -__all__ = ("StructuredFormatter", "correlation_id_var", "get_correlation_id", "get_logger", "set_correlation_id") |
| 15 | +__all__ = ( |
| 16 | + "SqlglotCommandFallbackFilter", |
| 17 | + "StructuredFormatter", |
| 18 | + "correlation_id_var", |
| 19 | + "get_correlation_id", |
| 20 | + "get_logger", |
| 21 | + "set_correlation_id", |
| 22 | + "suppress_erroneous_sqlglot_log_messages", |
| 23 | +) |
16 | 24 |
|
17 | 25 | correlation_id_var: "ContextVar[Optional[str]]" = ContextVar("correlation_id", default=None) |
18 | 26 |
|
@@ -86,6 +94,26 @@ def filter(self, record: LogRecord) -> bool: |
86 | 94 | return True |
87 | 95 |
|
88 | 96 |
|
| 97 | +class SqlglotCommandFallbackFilter(logging.Filter): |
| 98 | + """Filter to suppress sqlglot's confusing 'Falling back to Command' warning. |
| 99 | +
|
| 100 | + This filter suppresses the warning message that sqlglot emits when it |
| 101 | + encounters unsupported syntax and falls back to parsing as a Command. |
| 102 | + This is expected behavior in SQLSpec and the warning is confusing to users. |
| 103 | + """ |
| 104 | + |
| 105 | + def filter(self, record: LogRecord) -> bool: |
| 106 | + """Suppress the 'Falling back to Command' warning message. |
| 107 | +
|
| 108 | + Args: |
| 109 | + record: The log record to evaluate |
| 110 | +
|
| 111 | + Returns: |
| 112 | + False if the record contains the fallback warning, True otherwise |
| 113 | + """ |
| 114 | + return "Falling back to parsing as a 'Command'" not in record.getMessage() |
| 115 | + |
| 116 | + |
89 | 117 | def get_logger(name: "Optional[str]" = None) -> logging.Logger: |
90 | 118 | """Get a logger instance with standardized configuration. |
91 | 119 |
|
@@ -121,3 +149,15 @@ def log_with_context(logger: logging.Logger, level: int, message: str, **extra_f |
121 | 149 | record = logger.makeRecord(logger.name, level, "(unknown file)", 0, message, (), None) |
122 | 150 | record.extra_fields = extra_fields |
123 | 151 | logger.handle(record) |
| 152 | + |
| 153 | + |
| 154 | +def suppress_erroneous_sqlglot_log_messages() -> None: |
| 155 | + """Suppress confusing sqlglot warning messages. |
| 156 | +
|
| 157 | + Adds a filter to the sqlglot logger to suppress the warning message |
| 158 | + about falling back to parsing as a Command. This is expected behavior |
| 159 | + in SQLSpec and the warning is confusing to users. |
| 160 | + """ |
| 161 | + sqlglot_logger = logging.getLogger("sqlglot") |
| 162 | + if not any(isinstance(f, SqlglotCommandFallbackFilter) for f in sqlglot_logger.filters): |
| 163 | + sqlglot_logger.addFilter(SqlglotCommandFallbackFilter()) |
0 commit comments