Skip to content

Commit

Permalink
add set log level function
Browse files Browse the repository at this point in the history
  • Loading branch information
wyattliang committed Aug 23, 2022
1 parent 345c608 commit 174beb9
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
8 changes: 3 additions & 5 deletions qtaf_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
# -----------------------------------
DEBUG = False

LOG_LEVEL = 10
LOG_LEVEL = 10 # 默认DEBUG

# -----------------------------------
# 全局数据驱动配置
Expand All @@ -32,13 +32,11 @@
# 项目配置
# -----------------------------------
PROJECT_NAME = 'qtaf'
PROJECT_MODE = 'standalone' #choices: standard/standalone
PROJECT_ROOT = None#os.path.dirname(__file__)
PROJECT_MODE = 'standalone' # choices: standard/standalone
PROJECT_ROOT = None # os.path.dirname(__file__)
INSTALLED_APPS = []


# -----------------------------------
# Assert
# -----------------------------------
QTAF_REWRITE_ASSERT = True

18 changes: 14 additions & 4 deletions testbase/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,15 @@ def format(self, record):
return smart_binary(s, encoding=_encoding)


_stream_handler=logging.StreamHandler(_stream)
_stream_handler = logging.StreamHandler(_stream)
_stream_handler.terminator = b"\n"
_stream_handler.setFormatter(_Formatter())


class TestResultBridge(logging.Handler):
'''中转log信息到TestResult
'''

def emit(self, log_record):
'''Log Handle 必须实现此函数
'''
Expand All @@ -47,59 +48,68 @@ def emit(self, log_record):
return
record = {}
if log_record.exc_info:
record['traceback'] = ''.join(traceback.format_tb(log_record.exc_info[2])) + '%s: %s' %(
log_record.exc_info[0].__name__, log_record.exc_info[1])
record['traceback'] = ''.join(traceback.format_tb(log_record.exc_info[2])) + '%s: %s' % (
log_record.exc_info[0].__name__, log_record.exc_info[1])
testresult.log_record(log_record.levelno, log_record.msg, record)


_LOGGER_NAME = "QTA_LOGGER"
_logger = logging.getLogger(_LOGGER_NAME)
# _logger.setLevel(logging.DEBUG)
_logger.addHandler(TestResultBridge())


def critical(msg, *args, **kwargs):
_logger.error(msg, *args, **kwargs)


fatal = critical


def error(msg, *args, **kwargs):
'''Log a message with severity 'ERROR' on the root logger.
'''
_logger.error(msg, *args, **kwargs)


def exception(msg, *args):
'''Log a message with severity 'ERROR' on the root logger,with exception information.
'''
_logger.exception(msg, *args)


def warning(msg, *args, **kwargs):
'''Log a message with severity 'WARNING' on the root logger.
'''
_logger.warning(msg, *args, **kwargs)


warn = warning


def info(msg, *args, **kwargs):
'''Log a message with severity 'INFO' on the root logger.
'''
_logger.info(msg, *args, **kwargs)


def debug(msg, *args, **kwargs):
'''Log a message with severity 'DEBUG' on the root logger.
'''
_logger.debug(msg, *args, **kwargs)


def log(level, msg, *args, **kwargs):
'''Log 'msg % args' with the integer severity 'level' on the root logger.
'''
_logger.log(level, msg, *args, **kwargs)


def addHandler(hdlr):
'''Add the specified handler to this logger.
'''
_logger.addHandler(hdlr)


def removeHandler(hdlr):
'''Remove the specified handler from this logger.
'''
Expand Down

0 comments on commit 174beb9

Please sign in to comment.