Skip to content
Open
6 changes: 3 additions & 3 deletions qta_statics/qta-report.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion qtaf_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
PROJECT_MODE = 'standalone' #choices: standard/standalone
PROJECT_ROOT = None#os.path.dirname(__file__)
INSTALLED_APPS = []

QTAF_FAILED_SKIP_RUNTEST = True

# -----------------------------------
# Assert
Expand Down
67 changes: 50 additions & 17 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 @@ -18,19 +18,43 @@
import logging
import sys
import traceback
import os

import Testcase.settings as settings
from testbase import context
from testbase.util import ensure_binary_stream, smart_binary
from testbase.util import ensure_binary_stream, smart_binary, smart_text

_stream, _encoding = ensure_binary_stream(sys.stdout)
class PackagePathFilter(logging.Filter):
def filter(self, record):
pathname = record.pathname
record.relativepath = None
abs_sys_paths = map(os.path.abspath, sys.path)
for path in sorted(abs_sys_paths, key=len, reverse=True): # longer paths first
if not path.endswith(os.sep):
path += os.sep
if pathname.startswith(path):
record.relativepath = os.path.relpath(pathname, path)
break
return True

class _Formatter(logging.Formatter):
class _StreamFormatter(logging.Formatter):
def format(self, record):
s = super(_Formatter, self).format(record)
_f = logging.Formatter('[%(asctime)s][%(levelname)s][%(threadName)s][%(relativepath)s:%(lineno)d][%(funcName)s] %(message)s')
s = _f.format(record)
return smart_binary(s, encoding=_encoding)


class _ReportFormatter(logging.Formatter):
def format(self, record):
_f = logging.Formatter('[%(threadName)s][%(relativepath)s:%(lineno)d][%(funcName)s] %(message)s')
s = _f.format(record)
return smart_binary(s, encoding=_encoding)

_stream_handler=logging.StreamHandler(_stream)
_stream_handler.terminator = b"\n"
_stream_handler.setFormatter(_Formatter())
_stream_handler.setFormatter(_StreamFormatter())
_stream_handler.addFilter(PackagePathFilter())
_stream_handler.setLevel(logging.DEBUG)

class TestResultBridge(logging.Handler):
'''中转log信息到TestResult
Expand All @@ -46,17 +70,26 @@ def emit(self, log_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])
testresult.log_record(log_record.levelno, log_record.msg, record)

msg = smart_text(self.format(log_record))
testresult.log_record(log_record.levelno, msg, record)


result_handler = TestResultBridge()
result_handler.setLevel(logging.DEBUG if settings.DEBUG else logging.INFO)
result_handler.setFormatter(_ReportFormatter())
result_handler.addFilter(PackagePathFilter())

_LOGGER_NAME = "QTA_LOGGER"
_logger = logging.getLogger(_LOGGER_NAME)
_logger.setLevel(logging.DEBUG)
_logger.addHandler(TestResultBridge())
# if settings.CONSOLE_REPORT:
# _logger.addHandler(_stream_handler)
_logger.addHandler(result_handler)



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

fatal = critical

def error(msg, *args, **kwargs):
Expand Down Expand Up @@ -90,12 +123,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.
'''
Expand Down
2 changes: 1 addition & 1 deletion testbase/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -947,7 +947,7 @@ def end_report(self):
super(HtmlTestReport, self).end_report()
data = json.dumps(self._data)
content = "var qta_report_data = %s" % data
with codecs_open("qta-report.js", "w", encoding="utf-8") as fd:
with codecs_open("qta-report.js", "w", encoding="utf-8",buffering=-1) as fd:
fd.write(content)

qta_report_html = get_inner_resource("qta_statics", "qta-report.html")
Expand Down
Loading