import logging import logging.handlers import os import time def getLogger(): logger = logging.getLogger("") # 设置输出的等级 LEVELS = {'NOSET': logging.NOTSET, 'DEBUG': logging.DEBUG, 'INFO': logging.INFO, 'WARNING': logging.WARNING, 'ERROR': logging.ERROR, 'CRITICAL': logging.CRITICAL} # 创建文件目录 logs_dir = r"E:satislog" if os.path.exists(logs_dir) and os.path.isdir(logs_dir): pass else: os.mkdir(logs_dir) # 修改log保存位置 timestamp = time.strftime("%Y-%m-%d", time.localtime()) logfilename = '%s.txt' % timestamp logfilepath = os.path.join(logs_dir, logfilename) rotatingFileHandler = logging.handlers.RotatingFileHandler(filename=logfilepath, maxBytes=1024 * 1024 * 50, backupCount=5) # 设置输出格式 formatter = logging.Formatter('[%(asctime)s] [%(levelname)s] [%(filename)s] [%(lineno)d] %(message)s', '%Y-%m-%d %H:%M:%S') rotatingFileHandler.setFormatter(formatter) # 控制台句柄 console = logging.StreamHandler() console.setLevel(logging.NOTSET) console.setFormatter(formatter) # 添加内容到日志句柄中 logger.addHandler(rotatingFileHandler) logger.addHandler(console) logger.setLevel(logging.NOTSET) return logger logger = getLogger() if __name__ == '__main__': logger.info("this is info") logger.debug("this is debug") logger.error("this is error") logger.warning("this is warning")