封装logging 处理日志信息


日志在写框架的过程中非常重要,可以起到记录作用,配合封装其他操作,可以把每一步的执行操作都写到日志中

使用时,直接调用即可

import logging
import os
from logging.handlers import RotatingFileHandler
import time"""
日志
"""

formats = " %(asctime)s  %(levelname)s %(filename)s %(funcName)s [ line:%(lineno)d ] %(message)s"
datefmt = '%a, %d %b %Y %H:%M:%S'

# 控制台显示
handler_1 = logging.StreamHandler()
# 本地时间,格式年月日时分  strftime 将元组装换成字符串,localtime 当前时间
curTime = time.strftime("%Y-%m-%d %H-%M", time.localtime())
# 创建日志路径
log_path = os.path.join('日志文件放到哪个路径', f'Autotest_{curTime}.log')

handler_2 = RotatingFileHandler(log_path, backupCount=20, encoding='utf-8')
# 设置root logger 的输出内容形式,输出渠道
# format 日志格式、datefmt 时间格式、level日记级别、handlers
logging.basicConfig(format=formats, datefmt=datefmt, level=logging.DEBUG, handlers=[handler_1, handler_2])

if __name__ == '__main__':
logging.debug('日志')

使用了之后可以在指定的路径创建一个 log 文件


原文地址:https://www.cnblogs.com/yongzhuang/p/12183374.html