pixiv-spider/log.py

29 lines
1022 B
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import logging
import time
import config
def init_logger():
global logger
log_filename = '{}/pixiv-spider {}.log'.format(config.LOG_PATH, time.strftime("%Y-%m-%d %H%M%S", time.localtime()))
logger = logging.getLogger(__file__)
logger.setLevel(logging.DEBUG)
# 建立一个filehandler来把日志记录在文件里级别为debug以上
fh = logging.FileHandler(log_filename, encoding='UTF-8')
fh.setLevel(logging.INFO)
# 建立一个stream handler来把日志打在CMD窗口上级别为error以上
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
# 设置日志格式
formatter = logging.Formatter("%(asctime)s - %(filename)s[Line: %(lineno)d] - %(levelname)s: %(message)s",
datefmt="%Y-%m-%d %H:%M:%S")
ch.setFormatter(formatter)
fh.setFormatter(formatter)
# 将相应的handler添加在logger对象中
logger.addHandler(ch)
logger.addHandler(fh)
def get_logger():
return logging.getLogger(__file__)