Log unhandled exceptions stacktraces in log files

This commit is contained in:
euronion 2024-01-18 14:12:17 +01:00
parent 2f5f260c18
commit ecb0a1f79f
2 changed files with 11 additions and 0 deletions

View File

@ -18,6 +18,8 @@ Upcoming Release
* Add warning when BEV availability weekly profile has negative values in `build_transport_demand`. * 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) PyPSA-Eur 0.9.0 (5th January 2024)
================================== ==================================

View File

@ -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. Do (not) skip the default handlers created for redirecting output to STDERR and file.
""" """
import logging import logging
import sys
kwargs = snakemake.config.get("logging", dict()).copy() kwargs = snakemake.config.get("logging", dict()).copy()
kwargs.setdefault("level", "INFO") kwargs.setdefault("level", "INFO")
@ -102,7 +103,15 @@ def configure_logging(snakemake, skip_handlers=False):
} }
) )
logging.basicConfig(**kwargs) 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): def update_p_nom_max(n):
# if extendable carriers (solar/onwind/...) have capacity >= 0, # if extendable carriers (solar/onwind/...) have capacity >= 0,