Add timestamp, level and modulename to the format for capture log

Change-Id: I7f612203d2fe95181a7cd4093d889df745339b6a
This commit is contained in:
Federico Ressi 2020-08-07 15:56:28 +02:00
parent d4b26b98cf
commit 37d2ea4011
2 changed files with 15 additions and 3 deletions

View File

@ -30,17 +30,22 @@ class CaptureLogFixture(_fixture.SharedFixture):
level = None
logger = logging.root
handler = None
format = "%(asctime)-15s %(levelname)s %(name)s | %(message)s"
def __init__(self, test_case_id, logger=None, level=None):
def __init__(self, test_case_id, logger=None, level=None, fmt=None):
super(CaptureLogFixture, self).__init__()
self.test_case_id = test_case_id
if logger:
self.logger = logger
if level:
self.level = level
if fmt:
self.format = fmt
def setup_fixture(self):
self.handler = handler = CaptureLogHandler(level=self.level)
formatter = logging.Formatter(self.format)
handler.setFormatter(formatter)
self.addCleanup(self.logger.removeHandler, handler)
self.logger.addHandler(handler)
LOG.debug('--- BEGIN %s ---', self.test_case_id)

View File

@ -27,6 +27,13 @@ class CapureLogTest(unit.TobikoUnitTest):
def test_capture_log_content(self):
with tobiko.CaptureLogFixture(test_case_id=self.id()) as capture:
LOG.warning('Some debug line')
LOG.warning('< Some debug line>')
logged_lines = capture.getDetails()['log'].as_text().splitlines()
self.assertIn('Some debug line', logged_lines)
expected = f" WARNING {__name__} | < Some debug line>"
for line in logged_lines:
if expected in line:
break
else:
lines = '\n'.join(logged_lines)
self.fail(f"log line not captured: '{expected}' not in \n"
f"{lines}")