From 345c608f0a4fca33fe0268a34452e73f47d67c2a Mon Sep 17 00:00:00 2001 From: wyattliang Date: Tue, 23 Aug 2022 18:17:41 +0800 Subject: [PATCH 1/2] add set log level function --- qtaf_settings.py | 14 ++++++++------ testbase/conf.py | 4 ++++ testbase/logger.py | 28 ++++++++++++++++------------ 3 files changed, 28 insertions(+), 18 deletions(-) diff --git a/qtaf_settings.py b/qtaf_settings.py index c188e23..7e26e81 100644 --- a/qtaf_settings.py +++ b/qtaf_settings.py @@ -2,12 +2,12 @@ # # Tencent is pleased to support the open source community by making QTA available. # Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. -# Licensed under the BSD 3-Clause License (the "License"); you may not use this +# Licensed under the BSD 3-Clause License (the "License"); you may not use this # file except in compliance with the License. You may obtain a copy of the License at -# +# # https://opensource.org/licenses/BSD-3-Clause -# -# Unless required by applicable law or agreed to in writing, software distributed +# +# Unless required by applicable law or agreed to in writing, software distributed # under the License is distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS # OF ANY KIND, either express or implied. See the License for the specific language # governing permissions and limitations under the License. @@ -19,7 +19,9 @@ # 调试模式开关 # ----------------------------------- DEBUG = False - + +LOG_LEVEL = 10 + # ----------------------------------- # 全局数据驱动配置 # ----------------------------------- @@ -36,7 +38,7 @@ # ----------------------------------- -# Assert +# Assert # ----------------------------------- QTAF_REWRITE_ASSERT = True diff --git a/testbase/conf.py b/testbase/conf.py index 8848cc4..5f7c697 100644 --- a/testbase/conf.py +++ b/testbase/conf.py @@ -216,6 +216,10 @@ def __contain__(self, key): settings = _Settings() +logg_level = settings.LOG_LEVEL or 10 + +logger._logger.setLevel(logg_level) + class _InnerSettings(object): """inner settings for a SettingsMixin class diff --git a/testbase/logger.py b/testbase/logger.py index 7f264d8..3bd2716 100644 --- a/testbase/logger.py +++ b/testbase/logger.py @@ -2,12 +2,12 @@ # # Tencent is pleased to support the open source community by making QTA available. # Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. -# Licensed under the BSD 3-Clause License (the "License"); you may not use this +# Licensed under the BSD 3-Clause License (the "License"); you may not use this # file except in compliance with the License. You may obtain a copy of the License at -# +# # https://opensource.org/licenses/BSD-3-Clause -# -# Unless required by applicable law or agreed to in writing, software distributed +# +# Unless required by applicable law or agreed to in writing, software distributed # under the License is distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS # OF ANY KIND, either express or implied. See the License for the specific language # governing permissions and limitations under the License. @@ -20,18 +20,21 @@ import traceback from testbase import context from testbase.util import ensure_binary_stream, smart_binary - + _stream, _encoding = ensure_binary_stream(sys.stdout) + class _Formatter(logging.Formatter): def format(self, record): s = super(_Formatter, self).format(record) return smart_binary(s, encoding=_encoding) - + + _stream_handler=logging.StreamHandler(_stream) _stream_handler.terminator = b"\n" _stream_handler.setFormatter(_Formatter()) + class TestResultBridge(logging.Handler): '''中转log信息到TestResult ''' @@ -47,16 +50,17 @@ def emit(self, log_record): 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.setLevel(logging.DEBUG) _logger.addHandler(TestResultBridge()) - + def critical(msg, *args, **kwargs): _logger.error(msg, *args, **kwargs) - + fatal = critical def error(msg, *args, **kwargs): @@ -90,12 +94,12 @@ 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. ''' From 174beb9bba5805219108dde3ae1f981a1719ea8f Mon Sep 17 00:00:00 2001 From: wyattliang Date: Tue, 23 Aug 2022 18:25:53 +0800 Subject: [PATCH 2/2] add set log level function --- qtaf_settings.py | 8 +++----- testbase/logger.py | 18 ++++++++++++++---- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/qtaf_settings.py b/qtaf_settings.py index 7e26e81..269048e 100644 --- a/qtaf_settings.py +++ b/qtaf_settings.py @@ -20,7 +20,7 @@ # ----------------------------------- DEBUG = False -LOG_LEVEL = 10 +LOG_LEVEL = 10 # 默认DEBUG # ----------------------------------- # 全局数据驱动配置 @@ -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 - diff --git a/testbase/logger.py b/testbase/logger.py index 3bd2716..ccf2791 100644 --- a/testbase/logger.py +++ b/testbase/logger.py @@ -30,7 +30,7 @@ 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()) @@ -38,6 +38,7 @@ def format(self, record): class TestResultBridge(logging.Handler): '''中转log信息到TestResult ''' + def emit(self, log_record): '''Log Handle 必须实现此函数 ''' @@ -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. '''