diff --git a/doc/release_notes.rst b/doc/release_notes.rst index 44cea086..a48d1a4e 100644 --- a/doc/release_notes.rst +++ b/doc/release_notes.rst @@ -18,6 +18,8 @@ Upcoming Release * Add warning when BEV availability weekly profile has negative values in `build_transport_demand`. +* Stacktrace of uncaught exceptions should now be correctly included inside log files (via `configure_logging(..)`). + PyPSA-Eur 0.9.0 (5th January 2024) ================================== diff --git a/scripts/_helpers.py b/scripts/_helpers.py index e0982fd4..beca3f4f 100644 --- a/scripts/_helpers.py +++ b/scripts/_helpers.py @@ -80,6 +80,7 @@ def configure_logging(snakemake, skip_handlers=False): Do (not) skip the default handlers created for redirecting output to STDERR and file. """ import logging + import sys kwargs = snakemake.config.get("logging", dict()).copy() kwargs.setdefault("level", "INFO") @@ -102,7 +103,15 @@ def configure_logging(snakemake, skip_handlers=False): } ) logging.basicConfig(**kwargs) + + # Setup a function to handle uncaught exceptions and include them with their stacktrace into logfiles + def handle_exception(exc_type, exc_value, exc_traceback): + # Log the exception + logger = logging.getLogger() + logger.error("Uncaught exception", + exc_info=(exc_type, exc_value, exc_traceback)) + sys.excepthook = handle_exception def update_p_nom_max(n): # if extendable carriers (solar/onwind/...) have capacity >= 0,