diff --git a/notebooks/test/test-logging.ipynb b/notebooks/test/test-logging.ipynb index dc4a7b25d60776f1675049faa040237b4f48b029..606e6300c5dec36f66f0d88ed3e8be370a28c35b 100644 --- a/notebooks/test/test-logging.ipynb +++ b/notebooks/test/test-logging.ipynb @@ -2,7 +2,7 @@ "cells": [ { "cell_type": "code", - "execution_count": 1, + "execution_count": null, "id": "98e38fec", "metadata": {}, "outputs": [], @@ -55,9 +55,9 @@ "outputs": [], "source": [ "import warnings\n", + "from cal_tools.warnings import CalWarning\n", "\n", - "\n", - "class TestUserWarning(UserWarning):\n", + "class TestCalWarning(CalWarning):\n", " \"\"\"Base class for custom user warnings\"\"\"\n", " pass\n", "\n", @@ -67,7 +67,7 @@ " \n", "\n", "if 1 < 2:\n", - " warn_user('This inequality is true!', TestUserWarning)" + " warn_user('This inequality is true!', TestCalWarning)" ] }, { @@ -100,24 +100,14 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": null, "id": "c3b87719", "metadata": {}, - "outputs": [ - { - "ename": "ValueError", - "evalue": "FAIL", - "output_type": "error", - "traceback": [ - "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)", - "Cell \u001b[0;32mIn[2], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mFAIL\u001b[39m\u001b[38;5;124m'\u001b[39m)\n", - "\u001b[0;31mValueError\u001b[0m: FAIL" - ] - } - ], + "outputs": [], "source": [ - "raise ValueError('FAIL')" + "from cal_tools.exceptions import CalError\n", + "\n", + "raise CalError('Calibration Failure')" ] } ], diff --git a/src/xfel_calibrate/setup_logging.py b/src/xfel_calibrate/setup_logging.py index 28048f71c11f20ad76634ab5d41e289ff29cc020..56309b404e3bbfa626dcfe883c7589460d752f20 100644 --- a/src/xfel_calibrate/setup_logging.py +++ b/src/xfel_calibrate/setup_logging.py @@ -26,6 +26,20 @@ class CustomJsonFormatter(jsonlogger.JsonFormatter): log_record['filename'] = record.filename log_record['lineno'] = record.lineno log_record['class'] = getattr(record, 'class', 'DefaultClass') + + # Get the full class hierarchy + exc_class = getattr(record, 'exc_info', (None, None, None))[0] + if exc_class: + class_hierarchy = [] + current_class = exc_class + while current_class and current_class != Exception: + class_hierarchy.append(current_class.__name__) + current_class = current_class.__base__ + class_hierarchy.append('Exception') + log_record['class'] = '.'.join(reversed(class_hierarchy)) + else: + log_record['class'] = getattr(record, 'class', 'DefaultClass') + if record.exc_info: log_record['exc_info'] = self.formatException(record.exc_info) @@ -42,7 +56,7 @@ formatter = CustomJsonFormatter( # Function to create a file handler with job-specific JSON log file def create_job_specific_handler(log_level, file_suffix): - log_file = f'{file_suffix}_{JOB_ID}.json' + log_file = f'{file_suffix}_{JOB_ID}.log' handler = logging.FileHandler(log_file, delay=True) handler.setLevel(log_level) handler.setFormatter(formatter)