From 8e3908084d62d3393ea276c3504e142d4b565e19 Mon Sep 17 00:00:00 2001 From: AngusP Date: Tue, 23 Apr 2024 19:43:20 +0100 Subject: [PATCH] Improve logging for exceptions (e.g. TimeoutError) --- app/main.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/app/main.py b/app/main.py index 3c9be0c..fbc882f 100644 --- a/app/main.py +++ b/app/main.py @@ -452,6 +452,7 @@ async def base_exception_handler( status_code: int = 400, include_detail: bool = False, ) -> JSONResponse: + logger.exception(exc) if include_detail and request.app.state.settings.debug: reason = f"<{exc.__class__.__name__}>: {str(exc)}" elif include_detail: @@ -521,7 +522,7 @@ async def default_handler(request: Request, exc: Exception) -> JSONResponse: ) -def configure_logging(loglevel: str = "INFO"): +def configure_logging(loglevel: str = "INFO", backtrace: bool = False): logger.remove() intercept_logging() logger.add( @@ -532,6 +533,8 @@ def configure_logging(loglevel: str = "INFO"): "{time:%Y-%m-%d:%H:%m:%S} " "{level:9} {message}" ), + backtrace=backtrace, + diagnose=backtrace, ) @@ -541,7 +544,7 @@ def app_factory() -> FastAPI: if settings.is_test: settings = PhoenixdLNURLSettings(_env_file="test.env") # type: ignore - configure_logging(settings.log_level) + configure_logging(settings.log_level, settings.debug) logger.debug("Loaded settings: {settings}", settings=settings) if not settings.debug: