通过python封装日志
方式一:
通过python自带的logging模块进行封装
log 日志级别 NOTSET=0,DEBUG=10,INFO=20,WARNING=30,ERROR=40,CRITICAL=50
代码示例:
import time
import os
import logging
currrent_path = os.path.dirname(__file__)
log_path = os.path.join(currrent_path,'../logs')
class LogUtils:
def __init__(self,log_path=log_path):
self.logfile_path = log_path
# 创建日志对象logger
self.logger = logging.getLogger(__name__)
# 设置日志级别
self.logger.setLevel(level=logging.INFO)
# 设置日志的格式
formatter = logging.Formatter('%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s')
"""在log文件中输出日志"""
# 日志文件名称显示一天的日志
self.log_name_path = os.path.join(self.logfile_path, "UI_Test_%s" % time.strftime('%Y_%m_%d'))
# 创建文件处理程序并实现追加
self.file_log = logging.FileHandler(self.log_name_path, 'a', encoding='utf-8')
# 设置日志文件里的格式
self.file_
Python日志封装实践

最低0.47元/天 解锁文章
2727

被折叠的 条评论
为什么被折叠?



