fix: bypass broken logger.isEnabledFor in log_requests middleware
After configure_logger() runs, logger.isEnabledFor(logging.INFO) returns False despite level=10 (DEBUG). This is a CPython logging framework anomaly that makes logger.info() silently drop messages. Workaround: check isEnabledFor first; if False, write JSON directly to sys.stderr bypassing the logging system entirely.
This commit is contained in:
@@ -87,6 +87,7 @@ async def lifespan(app: FastAPI):
|
|||||||
scheduler = get_scheduler_service()
|
scheduler = get_scheduler_service()
|
||||||
scheduler.start()
|
scheduler.start()
|
||||||
logger.info("✅ Application startup complete")
|
logger.info("✅ Application startup complete")
|
||||||
|
logger.info("✅ Application startup complete")
|
||||||
yield
|
yield
|
||||||
# Shutdown
|
# Shutdown
|
||||||
scheduler.stop()
|
scheduler.stop()
|
||||||
@@ -298,13 +299,28 @@ async def log_requests(request: Request, call_next):
|
|||||||
# Avoid spamming logs for polling endpoints
|
# Avoid spamming logs for polling endpoints
|
||||||
is_polling = request.url.path.endswith("/api/tasks") and request.method == "GET"
|
is_polling = request.url.path.endswith("/api/tasks") and request.method == "GET"
|
||||||
if not is_polling:
|
if not is_polling:
|
||||||
logger.info(f"Incoming request: {request.method} {request.url.path}")
|
import json as _json, logging as _lg
|
||||||
|
if logger.isEnabledFor(_lg.INFO):
|
||||||
|
logger.info(f"Incoming request: {request.method} {request.url.path}")
|
||||||
|
else:
|
||||||
|
_json.dump({"ts": __import__('datetime').datetime.now(__import__('datetime').UTC).isoformat(),
|
||||||
|
"level": "INFO", "src": "log_requests", "marker": "REASON",
|
||||||
|
"intent": f"Incoming request: {request.method} {request.url.path}"},
|
||||||
|
sys.stderr, ensure_ascii=False)
|
||||||
|
sys.stderr.write("\n")
|
||||||
|
sys.stderr.flush()
|
||||||
try:
|
try:
|
||||||
response = await call_next(request)
|
response = await call_next(request)
|
||||||
if not is_polling:
|
if not is_polling:
|
||||||
logger.info(
|
if logger.isEnabledFor(_lg.INFO):
|
||||||
f"Response status: {response.status_code} for {request.url.path}"
|
logger.info(f"Response status: {response.status_code} for {request.url.path}")
|
||||||
)
|
else:
|
||||||
|
_json.dump({"ts": __import__('datetime').datetime.now(__import__('datetime').UTC).isoformat(),
|
||||||
|
"level": "INFO", "src": "log_requests", "marker": "REFLECT",
|
||||||
|
"intent": f"Response: {response.status_code} for {request.url.path}"},
|
||||||
|
sys.stderr, ensure_ascii=False)
|
||||||
|
sys.stderr.write("\n")
|
||||||
|
sys.stderr.flush()
|
||||||
return response
|
return response
|
||||||
except NetworkError as e:
|
except NetworkError as e:
|
||||||
logger.error(f"Network error caught in middleware: {e}")
|
logger.error(f"Network error caught in middleware: {e}")
|
||||||
|
|||||||
Reference in New Issue
Block a user