Spaces:
Running
Running
feat: Add logging filter to suppress non-critical ASGI errors
Browse files- Filter uvicorn.error and starlette loggers
- Suppress 'NoneType not callable' and 'Exception in ASGI' messages
- These errors come from malformed external requests, not our code
Co-authored-by: Cursor <cursoragent@cursor.com>
app.py
CHANGED
|
@@ -21,9 +21,43 @@ Usage:
|
|
| 21 |
"""
|
| 22 |
|
| 23 |
import os
|
|
|
|
| 24 |
import gradio as gr
|
| 25 |
from dotenv import load_dotenv
|
| 26 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
# Load environment variables
|
| 28 |
load_dotenv()
|
| 29 |
|
|
|
|
| 21 |
"""
|
| 22 |
|
| 23 |
import os
|
| 24 |
+
import logging
|
| 25 |
import gradio as gr
|
| 26 |
from dotenv import load_dotenv
|
| 27 |
|
| 28 |
+
|
| 29 |
+
# =============================================================================
|
| 30 |
+
# Logging Configuration - Filter out non-critical ASGI errors
|
| 31 |
+
# =============================================================================
|
| 32 |
+
class ASGIErrorFilter(logging.Filter):
|
| 33 |
+
"""Filter to suppress known non-critical ASGI/MCP errors.
|
| 34 |
+
|
| 35 |
+
These errors occur when external clients send malformed requests
|
| 36 |
+
or when health checks hit MCP endpoints. They don't affect functionality.
|
| 37 |
+
"""
|
| 38 |
+
|
| 39 |
+
SUPPRESSED_MESSAGES = [
|
| 40 |
+
"Exception in ASGI application",
|
| 41 |
+
"'NoneType' object is not callable",
|
| 42 |
+
"Exception Group Traceback",
|
| 43 |
+
]
|
| 44 |
+
|
| 45 |
+
def filter(self, record: logging.LogRecord) -> bool:
|
| 46 |
+
"""Return False to suppress the log record."""
|
| 47 |
+
message = record.getMessage()
|
| 48 |
+
for suppressed in self.SUPPRESSED_MESSAGES:
|
| 49 |
+
if suppressed in message:
|
| 50 |
+
return False
|
| 51 |
+
return True
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
# Apply filter to uvicorn error logger
|
| 55 |
+
uvicorn_error_logger = logging.getLogger("uvicorn.error")
|
| 56 |
+
uvicorn_error_logger.addFilter(ASGIErrorFilter())
|
| 57 |
+
|
| 58 |
+
# Also filter the root logger for starlette errors
|
| 59 |
+
logging.getLogger("starlette").addFilter(ASGIErrorFilter())
|
| 60 |
+
|
| 61 |
# Load environment variables
|
| 62 |
load_dotenv()
|
| 63 |
|