Skip to content
Snippets Groups Projects
Commit 906c4b04 authored by Karim Ahmed's avatar Karim Ahmed
Browse files

updates for nb test and setup logging

parent 2dc4de9d
No related branches found
No related tags found
1 merge request!1059[webservice] Intoduce Global Logger for xfel-calibrate
%% Cell type:code id:98e38fec tags:
``` python
in_folder = "./" # input folder
out_folder = "./" # output folder
```
%% Cell type:code id:7fbce574 tags:
``` python
import logging
```
%% Cell type:markdown id:ba0663ca tags:
## INFO/DEBUG
%% Cell type:code id:a3b5e455 tags:
``` python
logging.info("Logging some (INFO)rmation")
```
%% Cell type:markdown id:1bf72c57 tags:
## WARNINGS
%% Cell type:code id:c0d290a1 tags:
``` python
import warnings
from cal_tools.warnings import CalWarning
class TestUserWarning(UserWarning):
class TestCalWarning(CalWarning):
"""Base class for custom user warnings"""
pass
def warn_user(message, warning_class):
warnings.warn(message, warning_class)
if 1 < 2:
warn_user('This inequality is true!', TestUserWarning)
warn_user('This inequality is true!', TestCalWarning)
```
%% Cell type:code id:9b79ea47 tags:
``` python
logging.warning("This is a warning message using logging standard library.")
```
%% Cell type:markdown id:2b22e2e0 tags:
## ERRORS
%% Cell type:code id:0f3bfeb7 tags:
``` python
logging.error("Logging some (ERROR) without failing the notebook")
```
%% Cell type:code id:c3b87719 tags:
``` python
raise ValueError('FAIL')
```
from cal_tools.exceptions import CalError
%% Output
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Cell In[2], line 1
----> 1 raise ValueError('FAIL')
ValueError: FAIL
raise CalError('Calibration Failure')
```
......
......@@ -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)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment