Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
pycalibration
Manage
Activity
Members
Labels
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Model registry
Analyze
Contributor analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
calibration
pycalibration
Commits
25febf7a
Commit
25febf7a
authored
4 months ago
by
Karim Ahmed
Browse files
Options
Downloads
Patches
Plain Diff
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
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
notebooks/test/test-logging.ipynb
+7
-4
7 additions, 4 deletions
notebooks/test/test-logging.ipynb
src/xfel_calibrate/setup_logging.py
+33
-20
33 additions, 20 deletions
src/xfel_calibrate/setup_logging.py
with
40 additions
and
24 deletions
notebooks/test/test-logging.ipynb
+
7
−
4
View file @
25febf7a
...
...
@@ -36,6 +36,7 @@
"metadata": {},
"outputs": [],
"source": [
"# This wont be logged\n",
"logging.info(\"Logging some (INFO)rmation\")"
]
},
...
...
@@ -55,9 +56,9 @@
"outputs": [],
"source": [
"import warnings\n",
"from cal_tools.warnings import CalWarning\n",
"from cal_tools.warnings import Cal
ibration
Warning\n",
"\n",
"class TestCalWarning(CalWarning):\n",
"class TestCalWarning(Cal
ibration
Warning):\n",
" \"\"\"Base class for custom user warnings\"\"\"\n",
" pass\n",
"\n",
...
...
@@ -77,6 +78,7 @@
"metadata": {},
"outputs": [],
"source": [
"# This wont be logged\n",
"logging.warning(\"This is a warning message using logging standard library.\")"
]
},
...
...
@@ -95,6 +97,7 @@
"metadata": {},
"outputs": [],
"source": [
"# This wont be logged\n",
"logging.error(\"Logging some (ERROR) without failing the notebook\")"
]
},
...
...
@@ -105,9 +108,9 @@
"metadata": {},
"outputs": [],
"source": [
"from cal_tools.exceptions import CalError\n",
"from cal_tools.exceptions import Cal
ibration
Error\n",
"\n",
"raise CalError('Calibration Failure')"
"raise Cal
ibration
Error('Calibration Failure')"
]
}
],
...
...
%% 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
Cal
ibration
Warning
class
TestCalWarning
(
CalWarning
):
class
TestCalWarning
(
Cal
ibration
Warning
):
"""
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
Cal
ibration
Error
raise
CalError
(
'
Calibration Failure
'
)
raise
Cal
ibration
Error
(
'
Calibration Failure
'
)
```
...
...
This diff is collapsed.
Click to expand it.
src/xfel_calibrate/setup_logging.py
+
33
−
20
View file @
25febf7a
...
...
@@ -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
:
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment