范例是书写两个日志:错误日志(ERROR级别)和运行日志(DEBUG级别),其中运行日志每日凌晨进行分割
import logging,datetime,logging.handlers
from conf import settings
if __name__ == "__main__":
#两个日志,错误日志和运行日志,输出文件路径及文件名
error_log = settings.ERROR_LOG_FILE
run_log = settings.RUN_LOG_FILE
logger = logging.getLogger("mylog")
logger.setLevel(logging.DEBUG)
DATE_FORMAT = "%Y-%m-%d %H:%M:%S %p"
LOG_FORMAT = "%(asctime)s------%(levelname)s[:%(lineno)d]-------%(message)s"
#第一种普通写法
#file_run_log = logging.FileHandler(run_log)
#假如需要每日凌晨进行日志切割,注意导入模块时需要导入logging.handlers,否则报错
#when参数可以设置S M H D,分别是秒、分、小时、天分割,也可以按周几分割,也可以凌晨分割
file_run_log = rf_handler = logging.handlers.TimedRotatingFileHandler(run_log, when='midnight', interval=1, backupCount=7, atTime=datetime.time(0, 0, 0,