Skip to content

Commit

Permalink
Merge pull request #157 from alexwjxie/feature/result-log-formatter
Browse files Browse the repository at this point in the history
支持result日志中自定义formatter
  • Loading branch information
drunkdream committed Jul 24, 2023
2 parents 6b74312 + 249469d commit 588ac01
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
28 changes: 24 additions & 4 deletions testbase/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import logging
import sys
import traceback
import inspect
from testbase import context
from testbase.util import ensure_binary_stream, smart_binary

Expand All @@ -40,6 +41,7 @@ class TestResultBridge(logging.Handler):
def emit(self, log_record):
"""Log Handle 必须实现此函数"""
testresult = context.current_testresult()
formatted_msg = self.format(log_record)
if testresult is None:
_stream_handler.emit(log_record)
return
Expand All @@ -48,13 +50,14 @@ 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)
testresult.log_record(log_record.levelno, formatted_msg, record)


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


def critical(msg, *args, **kwargs):
Expand Down Expand Up @@ -109,11 +112,28 @@ def removeHandler(hdlr): # pylint: disable=invalid-name
def set_formatter(fmt):
"""Set the specified formatter to this logger.
"""
class __formatter(_Formatter):
class __Formatter(_Formatter):
def __init__(self, fmt):
super(_Formatter, self).__init__(fmt)

_stream_handler.setFormatter(__formatter(fmt))
class _CustomFormatter(logging.Formatter):
def format(self, record):
# Get the code line number and file name of the call logger function.
logger_module = "logger"
for frame_info in inspect.stack():
frame = frame_info[0]
module_name = inspect.getmodulename(frame.f_code.co_filename)
if module_name != "__init__" and module_name != logger_module:
caller = inspect.getframeinfo(frame)
break
else:
return super(_CustomFormatter, self).format(record)
record.filename = caller.filename.split('/')[-1]
record.lineno = caller.lineno
return super(_CustomFormatter, self).format(record)

_stream_handler.setFormatter(__Formatter(fmt))
_testresult_bridge.setFormatter(_CustomFormatter(fmt))

def set_level(level):
"""Set the specified log level to this logger.
Expand Down
3 changes: 3 additions & 0 deletions tests/test_testbase/test_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ def __init__(self, *args, **kwargs):
def emit(self, record):
self.messages.append(self.format(record))

def format(self, record):
return self.formatter.format(record)

class LoggerTest(unittest.TestCase):


Expand Down

0 comments on commit 588ac01

Please sign in to comment.