import configparser import logging from logging.handlers import TimedRotatingFileHandler import os BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) print(BASE_DIR) 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() class NoParsingFilter(logging.Filter): def filter(self, record): return 'pdfminer' not in record.name def getHandle(): for handler in logger.handlers: if isinstance(handler,logging.StreamHandler): return handler return logging.StreamHandler() def init(): logger.setLevel(logging.INFO) log_format=logging.Formatter(fmt="%(asctime)s %(levelname)s : %(message)s",datefmt='%Y-%m-%d %H:%M:%S') # 在控制台打印日志 streamHandler = getHandle() streamHandler.setFormatter(log_format) streamHandler.addFilter(NoParsingFilter()) logger.addHandler(streamHandler) logpath=BASE_DIR+"/log/" print(logpath) 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) timedRotatingFileHandler.addFilter(NoParsingFilter()) logger.addHandler(timedRotatingFileHandler)