File size: 1,528 Bytes
9dd3461 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
import time
import pathlib
import logging
from pkg_resources import resource_stream
from ruamel.yaml import YAML
yaml = YAML()
def get_default_conf(stream_args: list = None) -> dict:
if stream_args is None:
stream_args = ["cn2an", "config.yaml"]
with resource_stream(*stream_args) as stream:
return yaml.load(stream)
def get_logger(name: str = "cn2an", level: str = "info") -> logging.Logger:
logger = logging.getLogger(name)
level_dict = {
"debug": logging.DEBUG,
"info": logging.INFO,
"warning": logging.WARNING,
"error": logging.ERROR,
"critical": logging.CRITICAL
}
logger.setLevel(level_dict[level])
if not logger.handlers:
log_path = log_path_util()
fh = logging.FileHandler(log_path)
fh.setLevel(logging.INFO)
fh_fmt = logging.Formatter("%(asctime)-15s %(filename)s %(levelname)s %(lineno)d: %(message)s")
fh.setFormatter(fh_fmt)
console = logging.StreamHandler()
console.setLevel(logging.DEBUG)
console_fmt = logging.Formatter("%(filename)s %(levelname)s %(lineno)d: %(message)s")
console.setFormatter(console_fmt)
logger.addHandler(fh)
logger.addHandler(console)
return logger
def log_path_util(name: str = "cn2an") -> str:
day = time.strftime("%Y-%m-%d", time.localtime())
log_path = pathlib.Path(f"./log/{day}")
if not log_path.exists():
log_path.mkdir(parents=True)
return f"{str(log_path)}/{name}.log"
|