引言
使用python做自动化测试的时候,对于关键信息输出和记录是必要的,方便调试和记录查找。python有个logging模块,我们简单对其进行封装。
设计流程
话不多说,流程图已经画,自己领会。
源码
""" 封装log方法 """ import os import time import logging.handlers # 日志打印等级 LEVELS = { 'debug': logging.DEBUG, 'info': logging.INFO, 'warning': logging.WARNING, 'error': logging.ERROR, 'critical': logging.CRITICAL } # 创建一个日志 logger = logging.getLogger() level = 'default' # 创建日志文件方法 def create_file(filename): path = filename[0:filename.rfind('/')] if not os.path.isdir(path): os.makedirs(path) if not os.path.isfile(filename): fd = open(filename, mode='w', encoding='utf-8') fd.close() else: pass # 给logger添加handler 添加内容到日志句柄中 def set_handler(levels): if levels == 'error': logger.addHandler(MyLog.err_handler) logger.addHandler(MyLog.handler) # 在记录日志之后移除句柄 def remove_handler(levels): if levels == 'error': logger.removeHandler(MyLog.err_handler) logger.removeHandler(MyLog.handler) def get_current_time(): return time.strftime(MyLog.date, time.localtime(time.time())) class MyLog: path = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) log_file = path+'/Log/log.log' err_file = path+'/Log/err.log' logger.setLevel(LEVELS.get(level, logging.NOTSET)) create_file(log_file) create_file(err_file) date = '%Y-%m-%d %H:%M:%S' # 创建一个handler,用于写入日志文件 handler = logging.FileHandler(log_file, encoding='utf-8') err_handler = logging.FileHandler(err_file, encoding='utf-8') @staticmethod def debug(log_meg): set_handler('debug') # 文件中输出模式 logger.debug("[DEBUG " + get_current_time() + "]" + log_meg) remove_handler('debug') @staticmethod def info(log_meg): set_handler('info') logger.info("[INFO " + get_current_time() + "]" + log_meg) remove_handler('info') @staticmethod def warning(log_meg): set_handler('warning') logger.warning("[WARNING " + get_current_time() + "]" + log_meg) remove_handler('warning') @staticmethod def error(log_meg): set_handler('error') logger.error("[ERROR " + get_current_time() + "]" + log_meg) remove_handler('error') @staticmethod def critical(log_meg): set_handler('critical') logger.error("[CRITICAL " + get_current_time() + "]" + log_meg) remove_handler('critical') # 设置控制台输出格式 formatter = logging.Formatter( '[%(asctime)s] [%(levelname)s] %(message)s', '%Y-%m-%d %H:%M:%S') # 再创建一个handler,用于输出到控制台 console = logging.StreamHandler() console.setFormatter(formatter) logger.addHandler(console) console.setLevel(logging.NOTSET) if __name__ == "__main__": MyLog.debug("This is debug message") MyLog.info("This is info message") MyLog.warning("This is warning message") MyLog.error("This is error") MyLog.critical("This is critical message")
运行结果
[2020-05-23 20:41:10] [DEBUG] [DEBUG 2020-05-23 20:41:10]This is debug message [2020-05-23 20:41:10] [INFO] [INFO 2020-05-23 20:41:10]This is info message [2020-05-23 20:41:10] [WARNING] [WARNING 2020-05-23 20:41:10]This is warning message [2020-05-23 20:41:10] [ERROR] [ERROR 2020-05-23 20:41:10]This is error [2020-05-23 20:41:10] [ERROR] [CRITICAL 2020-05-23 20:41:10]This is critical message Process finished with exit code 0
文件结构:
文件内容:
总结
以上就是python日志的简单封装,基本上满足日常的需求。
另外,对测试开发,自动化测试技术与思想感兴趣的朋友,可以加入QQ测开交流群:696400122进行交流,互相学习与进步。不积跬步无以至千里!