diff options
-rw-r--r-- | NEWS | 3 | ||||
-rw-r--r-- | fixtures/_fixtures/logger.py | 8 | ||||
-rw-r--r-- | fixtures/tests/_fixtures/test_logger.py | 10 |
3 files changed, 19 insertions, 2 deletions
@@ -6,6 +6,9 @@ fixtures release notes NEXT ~~~~ +* FakeLogger now supports the ``datefmt`` parameter. + (Sean Dague) + * Fixtures source code is now hosted on `github <https://github.com/testing-cabal/fixtures>`_. (Robert Collins) diff --git a/fixtures/_fixtures/logger.py b/fixtures/_fixtures/logger.py index e46de3a..59b849f 100644 --- a/fixtures/_fixtures/logger.py +++ b/fixtures/_fixtures/logger.py @@ -65,13 +65,16 @@ class LogHandler(Fixture): class FakeLogger(Fixture): """Replace a logger and capture its output.""" - def __init__(self, name="", level=INFO, format=None, nuke_handlers=True): + def __init__(self, name="", level=INFO, format=None, + datefmt=None, nuke_handlers=True): """Create a FakeLogger fixture. :param name: The name of the logger to replace. Defaults to "". :param level: The log level to set, defaults to INFO. :param format: Logging format to use. Defaults to capturing supplied messages verbatim. + :param datefmt: Logging date format to use. + Mirrors the datefmt used in python loggging. :param nuke_handlers: If True remove all existing handles (prevents existing messages going to e.g. stdout). Defaults to True. @@ -86,6 +89,7 @@ class FakeLogger(Fixture): self._name = name self._level = level self._format = format + self._datefmt = datefmt self._nuke_handlers = nuke_handlers def setUp(self): @@ -95,7 +99,7 @@ class FakeLogger(Fixture): self._output = output handler = StreamHandler(output) if self._format: - handler.setFormatter(Formatter(self._format)) + handler.setFormatter(Formatter(self._format, self._datefmt)) self.useFixture( LogHandler(handler, name=self._name, level=self._level, nuke_handlers=self._nuke_handlers)) diff --git a/fixtures/tests/_fixtures/test_logger.py b/fixtures/tests/_fixtures/test_logger.py index 1f69459..bde29ec 100644 --- a/fixtures/tests/_fixtures/test_logger.py +++ b/fixtures/tests/_fixtures/test_logger.py @@ -14,6 +14,7 @@ # limitations under that license. import logging +import time from testtools import TestCase from testtools.compat import StringIO @@ -81,6 +82,15 @@ class FakeLoggerTest(TestCase, TestWithFixtures): logging.info("message") self.assertEqual("test_logger\n", fixture.output) + def test_custom_datefmt(self): + fixture = FakeLogger(format="%(asctime)s %(module)s", + datefmt="%Y") + self.useFixture(fixture) + logging.info("message") + self.assertEqual( + time.strftime("%Y test_logger\n", time.localtime()), + fixture.output) + def test_logging_output_included_in_details(self): fixture = FakeLogger() detail_name = "pythonlogging:''" |