Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/log level #128

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions qtaf_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -19,7 +19,9 @@
# 调试模式开关
# -----------------------------------
DEBUG = False


LOG_LEVEL = 10 # 默认DEBUG

# -----------------------------------
# 全局数据驱动配置
# -----------------------------------
Expand All @@ -30,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
# Assert
# -----------------------------------
QTAF_REWRITE_ASSERT = True

4 changes: 4 additions & 0 deletions testbase/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
44 changes: 29 additions & 15 deletions testbase/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -20,21 +20,25 @@
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 = 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 @@ -44,58 +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