diff --git a/NEWS b/NEWS index b9205c9..24ba02a 100644 --- a/NEWS +++ b/NEWS @@ -13,6 +13,9 @@ NEXT backport has significant bugfixes over older but still supported CPython stdlib versions. (Robert Collins) +* ``fixtures.FakeLogger`` now detects incorrectly formatted log messages. + (John Villalovos, #1503049) + 1.3.1 ~~~~~ diff --git a/fixtures/_fixtures/logger.py b/fixtures/_fixtures/logger.py index 0bf2e97..fe1c4cd 100644 --- a/fixtures/_fixtures/logger.py +++ b/fixtures/_fixtures/logger.py @@ -14,6 +14,8 @@ # limitations under that license. from logging import StreamHandler, getLogger, INFO, Formatter +import six +import sys from testtools.compat import _u @@ -61,6 +63,12 @@ class LogHandler(Fixture): self.addCleanup(logger.removeHandler, self.handler) +class StreamHandlerRaiseException(StreamHandler): + """Handler class that will raise an exception on formatting errors.""" + def handleError(self, record): + six.reraise(*sys.exc_info()) + + class FakeLogger(Fixture): """Replace a logger and capture its output.""" @@ -98,7 +106,7 @@ class FakeLogger(Fixture): name = _u("pythonlogging:'%s'") % self._name output = self.useFixture(StringStream(name)).stream self._output = output - handler = StreamHandler(output) + handler = StreamHandlerRaiseException(output) if self._format: formatter = (self._formatter or Formatter) handler.setFormatter(formatter(self._format, self._datefmt)) diff --git a/fixtures/tests/_fixtures/test_logger.py b/fixtures/tests/_fixtures/test_logger.py index caff986..f8e11cc 100644 --- a/fixtures/tests/_fixtures/test_logger.py +++ b/fixtures/tests/_fixtures/test_logger.py @@ -17,6 +17,7 @@ import logging import sys import time +import testtools from testtools import TestCase from testtools.compat import StringIO @@ -140,6 +141,11 @@ class FakeLoggerTest(TestCase, TestWithFixtures): except: pass + def test_exceptionraised(self): + with FakeLogger(): + with testtools.ExpectedException(TypeError): + logging.info("Some message", "wrongarg") + class LogHandlerTest(TestCase, TestWithFixtures):