From 948b05fdb71f7659f50554b8129868b84d79c038 Mon Sep 17 00:00:00 2001
From: ahmedk <karim.ahmed@xfel.eu>
Date: Mon, 7 Oct 2024 14:24:17 +0200
Subject: [PATCH] updates for nb test and setup logging

---
 notebooks/test/test-logging.ipynb   | 28 +++++++++-------------------
 src/xfel_calibrate/setup_logging.py | 16 +++++++++++++++-
 2 files changed, 24 insertions(+), 20 deletions(-)

diff --git a/notebooks/test/test-logging.ipynb b/notebooks/test/test-logging.ipynb
index dc4a7b25d..606e6300c 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 28048f71c..56309b404 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)
-- 
GitLab