Merge pull request #875 from PyPSA/feat/stacktrace-logging

Log unhandled exceptions stacktraces in log files
This commit is contained in:
Fabian Neumann 2024-01-18 18:16:32 +01:00 committed by GitHub
commit 6d85b549c4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 13 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(..)`).
* Cluster residential and services heat buses by default. Can be disabled with ``cluster_heat_buses: false``. * Cluster residential and services heat buses by default. Can be disabled with ``cluster_heat_buses: false``.
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")
@ -103,6 +104,16 @@ 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,