Commit 31ec7fa6 authored by BO ZHANG's avatar BO ZHANG 🏀
Browse files

added a configured logger

parent b4da1069
"""
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 ...")
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment