From 931720bc82aa725e10a6697be5f7f46f7e15e28c Mon Sep 17 00:00:00 2001
From: Thomas Kluyver <thomas.kluyver@xfel.eu>
Date: Fri, 10 Jan 2025 09:53:08 +0000
Subject: [PATCH] Only store CalibrationWarning & subclasses in warnings file

---
 src/xfel_calibrate/setup_logging.py | 15 +++++++--------
 1 file changed, 7 insertions(+), 8 deletions(-)

diff --git a/src/xfel_calibrate/setup_logging.py b/src/xfel_calibrate/setup_logging.py
index 8d1bf40ff..ae8cbda32 100644
--- a/src/xfel_calibrate/setup_logging.py
+++ b/src/xfel_calibrate/setup_logging.py
@@ -44,6 +44,7 @@ def log_error(
     except Exception as e:
         sys.stdout.write(f"Logging failed: {e}\n")
 
+original_showwarning = warnings.showwarning
 
 def handle_warning(
     message: Warning, category: Type[Warning],
@@ -52,20 +53,18 @@ def handle_warning(
     file=None, line=None,
 ) -> None:
     """Log and display warnings."""
-
     try:
-        warning_info = get_log_info(str(message), category)
+        if issubclass(category, CalibrationWarning):
+            warning_info = get_log_info(str(message), category)
 
-        with open(f"warnings_{JOB_ID}.log", "a") as log_file:
-            log_file.write(json.dumps(warning_info) + "\n")
+            with open(f"warnings_{JOB_ID}.log", "a") as log_file:
+                log_file.write(json.dumps(warning_info) + "\n")
 
     except Exception as e:
-        sys.stdout.write(f"Warning logging failed: {e}\n")
+        sys.stderr.write(f"Warning logging failed: {e}\n")
     finally:
         # Ensure warning is displayed in notebook.
-        # TODO: use e.g. latex_warning from cal_tools
-        # to display colored warning textbox
-        print(f"\n{category.__name__}: {message}\n")
+        original_showwarning(message, category, filename, lineno, file, line)
 
 
 # Get IPython shell
-- 
GitLab