Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
csst-pipeline
csst_common
Commits
31ec7fa6
Commit
31ec7fa6
authored
Oct 04, 2022
by
BO ZHANG
🏀
Browse files
added a configured logger
parent
b4da1069
Changes
1
Hide whitespace changes
Inline
Side-by-side
csst_common/logger.py
0 → 100644
View file @
31ec7fa6
"""
Identifier: KSC-SJ4-csst_common/logger.py
Name: logger.py
Description: CSST logger
Author: Bo Zhang
Created: 2022-10-04
Modified-History:
2022-10-04, Bo Zhang, created
2022-10-04, Bo Zhang, added get_logger
"""
import
logging
def
get_logger
(
name
=
"CSST MS L1 Pipeline"
,
filename
=
""
):
""" get a logger for CSST L1 Pipeline
Messages with levels >= DEBUG will be output by stream.
Messages with levels >= INFO will be recorded by a log file.
Parameters
----------
name: str
logger name
filename: str
log file name
Returns
-------
References
----------
for logging: https://docs.python.org/3/library/logging.html
for logging levels, conf: https://docs.python.org/3/library/logging.html#logging-levels
"""
# initialize logger
logger
=
logging
.
getLogger
(
name
=
name
)
logger
.
setLevel
(
logging
.
DEBUG
)
# logging formatter
formatter
=
logging
.
Formatter
(
"%(asctime)s - %(levelname)s - %(module)s.py:%(lineno)d - %(message)s"
)
# stream handler
stream_handler
=
logging
.
StreamHandler
()
stream_handler
.
setFormatter
(
formatter
)
stream_handler
.
setLevel
(
logging
.
DEBUG
)
logger
.
addHandler
(
stream_handler
)
# file handler
if
filename
!=
""
:
file_handler
=
logging
.
FileHandler
(
filename
=
filename
,
encoding
=
"utf-8"
)
file_handler
.
setFormatter
(
formatter
)
file_handler
.
setLevel
(
logging
.
INFO
)
logger
.
addHandler
(
file_handler
)
return
logger
if
__name__
==
"__main__"
:
import
os
print
(
os
.
getcwd
())
logger
=
get_logger
(
filename
=
"test_logger.log"
)
print
(
type
(
logger
),
logger
.
name
)
logger
.
debug
(
"Well .."
)
logger
.
info
(
"Hello ..."
)
logger
.
warning
(
"There ..."
)
logger
.
error
(
"Good morning ..."
)
Write
Preview
Supports
Markdown
0%
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!
Cancel
Please
register
or
sign in
to comment