当前位置 : 主页 > 网络安全 > 测试自动化 >

AppUi自动化框架logger.py代码

来源:互联网 收集:自由互联 发布时间:2021-06-19
from logging import handlers import logging from lib.path import SYSTEMPATH class Logger(object): __instance = None def __new__(cls, *args, **kwargs): if not Logger.__instance: Logger.__instance = object.__new__(cls, *args) return Logger.__
from logging import handlers
import logging
from lib.path import SYSTEMPATH

class Logger(object):
__instance = None

def __new__(cls, *args, **kwargs):
if not Logger.__instance:
Logger.__instance = object.__new__(cls, *args)
return Logger.__instance

def __init__(self):
# 格式化log的模板
self.formater = logging.Formatter(
‘[%(asctime)s] [%(levelname)s] [%(filename)s:%(funcName)s:%(lineno)d] %(message)s‘)

# 声明一个log对象
self.logger = logging.getLogger(‘log‘)
# 设置全局log级别
self.logger.setLevel(logging.DEBUG)

# 文件log
self.filelogger = handlers.RotatingFileHandler(SYSTEMPATH,
maxBytes=5242880,
backupCount=3
)
# 屏幕log
self.console = logging.StreamHandler()
# 对屏幕设置级别
self.console.setLevel(logging.DEBUG)
self.filelogger.setFormatter(self.formater)
self.console.setFormatter(self.formater)
self.logger.addHandler(self.filelogger)
self.logger.addHandler(self.console)

def log(self):
return self.logger

logger = Logger().log()
网友评论