Use records.clear() in LogCaptureHandler when caplog.clear()
This commit is contained in:
parent
c7e784f95d
commit
f29f79e39f
|
@ -40,7 +40,6 @@ if TYPE_CHECKING:
|
|||
else:
|
||||
logging_StreamHandler = logging.StreamHandler
|
||||
|
||||
|
||||
DEFAULT_LOG_FORMAT = "%(levelname)-8s %(name)s:%(filename)s:%(lineno)d %(message)s"
|
||||
DEFAULT_LOG_DATE_FORMAT = "%H:%M:%S"
|
||||
_ANSI_ESCAPE_SEQ = re.compile(r"\x1b\[[\d;]+m")
|
||||
|
@ -335,16 +334,6 @@ class LogCaptureHandler(logging_StreamHandler):
|
|||
"""Create a new log handler."""
|
||||
super().__init__(StringIO())
|
||||
self.records: List[logging.LogRecord] = []
|
||||
self.set_when(None)
|
||||
|
||||
def set_when(self, when: Optional[str]) -> None:
|
||||
"""Prepare for the given test phase (setup/call/teardown)."""
|
||||
self._when = when
|
||||
|
||||
def get_when(self) -> Optional[str]:
|
||||
return self._when
|
||||
|
||||
when = property(get_when, set_when)
|
||||
|
||||
def emit(self, record: logging.LogRecord) -> None:
|
||||
"""Keep the log records in a list in addition to the log text."""
|
||||
|
@ -355,6 +344,10 @@ class LogCaptureHandler(logging_StreamHandler):
|
|||
self.records = []
|
||||
self.stream = StringIO()
|
||||
|
||||
def clear(self) -> None:
|
||||
self.records.clear()
|
||||
self.stream = StringIO()
|
||||
|
||||
def handleError(self, record: logging.LogRecord) -> None:
|
||||
if logging.raiseExceptions:
|
||||
# Fail the test if the log message is bad (emit failed).
|
||||
|
@ -450,8 +443,7 @@ class LogCaptureFixture:
|
|||
|
||||
def clear(self) -> None:
|
||||
"""Reset the list of log records and the captured log text."""
|
||||
self.handler.reset()
|
||||
self._item.stash[caplog_records_key][self.handler.when] = self.records
|
||||
self.handler.clear()
|
||||
|
||||
def set_level(self, level: Union[int, str], logger: Optional[str] = None) -> None:
|
||||
"""Set the level of a logger for the duration of a test.
|
||||
|
@ -706,7 +698,6 @@ class LoggingPlugin:
|
|||
level=self.log_level,
|
||||
) as report_handler:
|
||||
caplog_handler.reset()
|
||||
caplog_handler.set_when(when)
|
||||
report_handler.reset()
|
||||
item.stash[caplog_records_key][when] = caplog_handler.records
|
||||
item.stash[caplog_handler_key] = caplog_handler
|
||||
|
|
|
@ -184,6 +184,11 @@ def test_clear_for_call_stage(caplog, logging_during_setup_and_teardown):
|
|||
assert [x.message for x in caplog.get_records("setup")] == ["a_setup_log"]
|
||||
assert set(caplog._item.stash[caplog_records_key]) == {"setup", "call"}
|
||||
|
||||
logging.info("a_call_log_after_clear")
|
||||
assert [x.message for x in caplog.get_records("call")] == ["a_call_log_after_clear"]
|
||||
assert [x.message for x in caplog.get_records("setup")] == ["a_setup_log"]
|
||||
assert set(caplog._item.stash[caplog_records_key]) == {"setup", "call"}
|
||||
|
||||
|
||||
def test_ini_controls_global_log_level(pytester: Pytester) -> None:
|
||||
pytester.makepyfile(
|
||||
|
|
Loading…
Reference in New Issue