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
948b05fd
Commit
948b05fd
authored
5 months ago
by
Karim Ahmed
Browse files
Options
Downloads
Patches
Plain Diff
updates for nb test and setup logging
parent
cccd2152
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
notebooks/test/test-logging.ipynb
+9
-19
9 additions, 19 deletions
notebooks/test/test-logging.ipynb
src/xfel_calibrate/setup_logging.py
+15
-1
15 additions, 1 deletion
src/xfel_calibrate/setup_logging.py
with
24 additions
and
20 deletions
notebooks/test/test-logging.ipynb
+
9
−
19
View file @
948b05fd
...
@@ -2,7 +2,7 @@
...
@@ -2,7 +2,7 @@
"cells": [
"cells": [
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count":
1
,
"execution_count":
null
,
"id": "98e38fec",
"id": "98e38fec",
"metadata": {},
"metadata": {},
"outputs": [],
"outputs": [],
...
@@ -55,9 +55,9 @@
...
@@ -55,9 +55,9 @@
"outputs": [],
"outputs": [],
"source": [
"source": [
"import warnings\n",
"import warnings\n",
"from cal_tools.warnings import CalWarning\n",
"\n",
"\n",
"\n",
"class TestCalWarning(CalWarning):\n",
"class TestUserWarning(UserWarning):\n",
" \"\"\"Base class for custom user warnings\"\"\"\n",
" \"\"\"Base class for custom user warnings\"\"\"\n",
" pass\n",
" pass\n",
"\n",
"\n",
...
@@ -67,7 +67,7 @@
...
@@ -67,7 +67,7 @@
" \n",
" \n",
"\n",
"\n",
"if 1 < 2:\n",
"if 1 < 2:\n",
" warn_user('This inequality is true!', Test
User
Warning)"
" warn_user('This inequality is true!', Test
Cal
Warning)"
]
]
},
},
{
{
...
@@ -100,24 +100,14 @@
...
@@ -100,24 +100,14 @@
},
},
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count":
2
,
"execution_count":
null
,
"id": "c3b87719",
"id": "c3b87719",
"metadata": {},
"metadata": {},
"outputs": [
"outputs": [],
{
"ename": "ValueError",
"evalue": "FAIL",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[0;32mIn[2], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mFAIL\u001b[39m\u001b[38;5;124m'\u001b[39m)\n",
"\u001b[0;31mValueError\u001b[0m: FAIL"
]
}
],
"source": [
"source": [
"raise ValueError('FAIL')"
"from cal_tools.exceptions import CalError\n",
"\n",
"raise CalError('Calibration Failure')"
]
]
}
}
],
],
...
...
%% 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!
'
,
Test
User
Warning
)
warn_user
(
'
This inequality is true!
'
,
Test
Cal
Warning
)
```
```
%% 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
...
...
This diff is collapsed.
Click to expand it.
src/xfel_calibrate/setup_logging.py
+
15
−
1
View file @
948b05fd
...
@@ -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
)
...
...
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