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

log only exceptions/warnings

parent 27a39159
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
# This wont be logged
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
from cal_tools.warnings import CalibrationWarning
class TestCalWarning(CalWarning):
class TestCalWarning(CalibrationWarning):
"""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!', TestCalWarning)
```
%% Cell type:code id:9b79ea47 tags:
``` python
# This wont be logged
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
# This wont be logged
logging.error("Logging some (ERROR) without failing the notebook")
```
%% Cell type:code id:c3b87719 tags:
``` python
from cal_tools.exceptions import CalError
from cal_tools.exceptions import CalibrationError
raise CalError('Calibration Failure')
raise CalibrationError('Calibration Failure')
```
......
......@@ -12,9 +12,22 @@ JOB_ID = os.getenv('SLURM_JOB_ID', 'local')
class ContextFilter(logging.Filter):
def filter(self, record):
record.notebook = NOTEBOOK_NAME
record.job_id = JOB_ID
return True
# Only allow records that come from exception handlers
if getattr(record, 'from_exception_handler', False):
record.notebook = NOTEBOOK_NAME
record.job_id = JOB_ID
return True
return False
def get_class_hierarchy(cls):
"""Helper function to get the full class hierarchy"""
class_hierarchy = []
current_class = cls
while current_class and current_class != object:
class_hierarchy.append(current_class.__name__)
current_class = current_class.__base__
return '.'.join(reversed(class_hierarchy))
class CustomJsonFormatter(jsonlogger.JsonFormatter):
......@@ -25,23 +38,15 @@ class CustomJsonFormatter(jsonlogger.JsonFormatter):
log_record['level'] = record.levelname
log_record['filename'] = record.filename
log_record['lineno'] = record.lineno
log_record['class'] = getattr(record, 'class', 'DefaultClass')
exc_info = getattr(record, 'exc_info', None)
if exc_info and exc_info[0]:
exc_class = exc_info[0]
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')
# Get log_class from extra parameters (set in our warning/error handlers)
if hasattr(record, 'log_class'):
log_record['log_class'] = record.log_class
if record.exc_info:
log_record['exc_info'] = self.formatException(record.exc_info)
exc_class = record.exc_info[0]
log_record['class'] = get_class_hierarchy(exc_class)
# Create a logger
......@@ -51,7 +56,7 @@ logger.setLevel(logging.INFO)
# Define a custom JSON format
formatter = CustomJsonFormatter(
'%(timestamp)s %(level)s %(filename)s %(lineno)d '
'%(notebook)s %(job_id)s %(class)s %(message)s')
'%(notebook)s %(job_id)s %(log_class)s %(message)s')
# Function to create a file handler with job-specific JSON log file
......@@ -91,6 +96,12 @@ handling_error = False
def safe_handle_error(exc_type, exc_value, exc_traceback):
global handling_error
# Added this block to skip sys.exit()
if exc_type in (SystemExit, KeyboardInterrupt):
sys.__excepthook__(exc_type, exc_value, exc_traceback)
return
if handling_error: # Avoid infinite loop of errors.
sys.stderr.write("Recursive error detected!\n")
traceback.print_exception(
......@@ -103,7 +114,8 @@ def safe_handle_error(exc_type, exc_value, exc_traceback):
extra={
'notebook': NOTEBOOK_NAME,
'job_id': JOB_ID,
'class': exc_type.__name__ if exc_type else 'DefaultErrorClass', # noqa
'log_class': get_class_hierarchy(exc_type),
'from_exception_handler': True
},
exc_info=(exc_type, exc_value, exc_traceback)
)
......@@ -123,7 +135,8 @@ def handle_warning(message, category, filename, lineno, file=None, line=None):
extra={
'notebook': NOTEBOOK_NAME,
'job_id': JOB_ID,
'class': category.__name__ if category else 'DefaultWarningClass', # noqa
'log_class': get_class_hierarchy(category),
'from_exception_handler': True
}
)
except Exception as log_error:
......
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