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

refactor: Small refactors on what log info included

- Remove object from parent class
- Reduce code duplication
- Remove log level and job id from log info
parent 2c9540e8
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: %% 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
import warnings import warnings
from cal_tools.warnings import CalibrationWarning from cal_tools.warnings import CalibrationWarning
``` ```
%% 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
class TestCalWarning(CalibrationWarning): class TestCalWarning(CalibrationWarning):
"""Base class for custom user warnings""" """Base class for custom user warnings"""
pass pass
if 1 < 2: if 1 < 2:
warnings.warn('This inequality is true!', TestCalWarning) warnings.warn('This inequality is true!', TestCalWarning)
``` ```
%% Cell type:code id:6a4d4f01 tags: %% Cell type:code id:6a4d4f01 tags:
``` python ``` python
warnings.warn('This user warning will be ignored', UserWarning) warnings.warn('This user warning will be ignored', UserWarning)
``` ```
%% Cell type:code id:9b79ea47 tags: %% Cell type:code id:9b79ea47 tags:
``` python ``` python
# This wont be logged # This wont be logged
logging.warning("This is a warning message using logging standard library. It wont be logged") logging.warning("This is a warning message using logging standard library. It wont be logged")
``` ```
%% 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
# This wont be logged # This wont be logged
logging.error("Logging some (ERROR) without failing the notebook. It wont be logged") logging.error("Logging some (ERROR) without failing the notebook. It wont be logged")
``` ```
%% Cell type:code id:c3b87719 tags: %% Cell type:code id:c3b87719 tags:
``` python ``` python
from cal_tools.exceptions import CalibrationError from cal_tools.exceptions import CalibrationError
raise CalibrationError('Calibration Failure') raise CalibrationError('Testing Calibration Failure!')
``` ```
......
...@@ -9,9 +9,24 @@ from typing import Type, Any ...@@ -9,9 +9,24 @@ from typing import Type, Any
from cal_tools.warnings import CalibrationWarning from cal_tools.warnings import CalibrationWarning
JOB_ID = os.getenv('SLURM_JOB_ID', 'local')
def get_class_hierarchy(cls: Type) -> str: def get_class_hierarchy(cls: Type) -> str:
"""Get the full class hierarchy of a class.""" """Get the full class hierarchy of a class."""
return '.'.join(c.__name__ for c in cls.__mro__) return ".".join(c.__name__ for c in cls.__mro__ if c != object)
def get_log_info(
message: str,
log_type: Type[Exception] | Type[Warning]
) -> dict:
"""Create a dictionary with log information."""
return {
"timestamp": datetime.now().isoformat(),
"message": message,
"class": get_class_hierarchy(log_type),
}
def log_error( def log_error(
...@@ -21,16 +36,10 @@ def log_error( ...@@ -21,16 +36,10 @@ def log_error(
) -> None: ) -> None:
"""Log error information to file.""" """Log error information to file."""
try: try:
error_info = { error_info = get_log_info(str(exc_value), exc_type)
"timestamp": datetime.now().isoformat(),
"job_id": os.getenv('SLURM_JOB_ID', 'local'),
"level": "ERROR",
"message": str(exc_value),
"class": get_class_hierarchy(exc_type),
}
with open(f"errors_{error_info['job_id']}.log", "a") as error_log_file: with open(f"errors_{JOB_ID}.log", "a") as log_file:
error_log_file.write(json.dumps(error_info) + "\n") log_file.write(json.dumps(error_info) + "\n")
except Exception as e: except Exception as e:
sys.stdout.write(f"Logging failed: {e}\n") sys.stdout.write(f"Logging failed: {e}\n")
...@@ -45,18 +54,10 @@ def handle_warning( ...@@ -45,18 +54,10 @@ def handle_warning(
"""Log and display warnings.""" """Log and display warnings."""
try: try:
warning_info = { warning_info = get_log_info(str(message), category)
"timestamp": datetime.now().isoformat(),
"job_id": os.getenv('SLURM_JOB_ID', 'local'), with open(f"warnings_{JOB_ID}.log", "a") as log_file:
"level": "WARNING", log_file.write(json.dumps(warning_info) + "\n")
"message": str(message),
"class": get_class_hierarchy(category),
}
with open(
f"warnings_{warning_info['job_id']}.log", "a"
) as warning_log_file:
warning_log_file.write(json.dumps(warning_info) + "\n")
except Exception as e: except Exception as e:
sys.stdout.write(f"Warning logging failed: {e}\n") sys.stdout.write(f"Warning logging failed: {e}\n")
......
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