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

updates for nb test and setup logging

parent cccd2152
No related branches found
No related tags found
No related merge requests found
%% Cell type:code id:98e38fec tags: %% Cell type:code id:98e38fec tags:
``` python ``` python
in_folder = "./" # input folder in_folder = "./" # input folder
out_folder = "./" # output folder out_folder = "./" # output folder
``` ```
%% Cell type:code id:7fbce574 tags: %% Cell type:code id:7fbce574 tags:
``` python ``` python
import logging import logging
``` ```
%% Cell type:markdown id:ba0663ca tags: %% Cell type:markdown id:ba0663ca tags:
## INFO/DEBUG ## INFO/DEBUG
%% Cell type:code id:a3b5e455 tags: %% Cell type:code id:a3b5e455 tags:
``` python ``` python
logging.info("Logging some (INFO)rmation") logging.info("Logging some (INFO)rmation")
``` ```
%% Cell type:markdown id:1bf72c57 tags: %% Cell type:markdown id:1bf72c57 tags:
## WARNINGS ## WARNINGS
%% Cell type:code id:c0d290a1 tags: %% Cell type:code id:c0d290a1 tags:
``` python ``` python
import warnings import warnings
from cal_tools.warnings import CalWarning
class TestCalWarning(CalWarning):
class TestUserWarning(UserWarning):
"""Base class for custom user warnings""" """Base class for custom user warnings"""
pass pass
def warn_user(message, warning_class): def warn_user(message, warning_class):
warnings.warn(message, warning_class) warnings.warn(message, warning_class)
if 1 < 2: if 1 < 2:
warn_user('This inequality is true!', TestUserWarning) warn_user('This inequality is true!', TestCalWarning)
``` ```
%% Cell type:code id:9b79ea47 tags: %% Cell type:code id:9b79ea47 tags:
``` python ``` python
logging.warning("This is a warning message using logging standard library.") logging.warning("This is a warning message using logging standard library.")
``` ```
%% Cell type:markdown id:2b22e2e0 tags: %% Cell type:markdown id:2b22e2e0 tags:
## ERRORS ## ERRORS
%% Cell type:code id:0f3bfeb7 tags: %% Cell type:code id:0f3bfeb7 tags:
``` python ``` python
logging.error("Logging some (ERROR) without failing the notebook") logging.error("Logging some (ERROR) without failing the notebook")
``` ```
%% Cell type:code id:c3b87719 tags: %% Cell type:code id:c3b87719 tags:
``` python ``` python
raise ValueError('FAIL') from cal_tools.exceptions import CalError
```
%% Output raise CalError('Calibration Failure')
```
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Cell In[2], line 1
----> 1 raise ValueError('FAIL')
ValueError: FAIL
......
...@@ -26,6 +26,20 @@ class CustomJsonFormatter(jsonlogger.JsonFormatter): ...@@ -26,6 +26,20 @@ class CustomJsonFormatter(jsonlogger.JsonFormatter):
log_record['filename'] = record.filename log_record['filename'] = record.filename
log_record['lineno'] = record.lineno log_record['lineno'] = record.lineno
log_record['class'] = getattr(record, 'class', 'DefaultClass') 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: if record.exc_info:
log_record['exc_info'] = self.formatException(record.exc_info) log_record['exc_info'] = self.formatException(record.exc_info)
...@@ -42,7 +56,7 @@ formatter = CustomJsonFormatter( ...@@ -42,7 +56,7 @@ formatter = CustomJsonFormatter(
# Function to create a file handler with job-specific JSON log file # Function to create a file handler with job-specific JSON log file
def create_job_specific_handler(log_level, file_suffix): 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 = logging.FileHandler(log_file, delay=True)
handler.setLevel(log_level) handler.setLevel(log_level)
handler.setFormatter(formatter) 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