import configparser import logging from logging.handlers import TimedRotatingFileHandler import os BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) cf = configparser.ConfigParser() config_path=BASE_DIR+"//config.ini" if not os.path.exists(config_path): raise Exception("配置文件:%s不存在" % config_path) cf.read(config_path,encoding='utf-8') logFile = cf.get('file', 'logFile') logger=logging.getLogger() logger.setLevel(logging.INFO) def init(): log_format=logging.Formatter(fmt="%(asctime)s %(levelname)s : %(message)s",datefmt='%Y-%m-%d %H:%M:%S') # 在控制台打印日志 streamHandler = logging.StreamHandler() streamHandler.setFormatter(log_format) logger.addHandler(streamHandler) logpath=BASE_DIR+"\\log\\" if not os.path.exists(BASE_DIR+"\\log\\"): os.mkdir(logpath) timedRotatingFileHandler=TimedRotatingFileHandler(filename=logpath+"all.log",when='H',interval=1,encoding='utf-8') timedRotatingFileHandler.setFormatter(log_format) logger.addHandler(timedRotatingFileHandler)