Have FakeLogger detect bad calls to logger
Affects | Status | Importance | Assigned to | Milestone | |
---|---|---|---|---|---|
Python Fixtures |
Fix Released
|
Medium
|
John L. Villalovos |
Bug Description
The logging module does not raise errors that can be caught. So if code logs a message with bad arguments we can't detect the error in unit tests.
See:
>>> import fixtures
>>> import logging
>>> logging.
>>> f = fixtures.
>>> f.setUp()
>>> logging.warn("foo")
>>> f.output
u'foo\n'
>>> logging.warn("foo", "bar")
Traceback (most recent call last):
File "/usr/lib/
msg = self.format(record)
File "/usr/lib/
return fmt.format(record)
File "/usr/lib/
record.message = record.getMessage()
File "/usr/lib/
msg = msg % self.args
TypeError: not all arguments converted during string formatting
Logged from file <stdin>, line 1
>>> try:
... logging.warn("foo", "bar")
... except Exception:
... print("failure")
...
Traceback (most recent call last):
File "/usr/lib/
msg = self.format(record)
File "/usr/lib/
return fmt.format(record)
File "/usr/lib/
record.message = record.getMessage()
File "/usr/lib/
msg = msg % self.args
TypeError: not all arguments converted during string formatting
Logged from file <stdin>, line 2
Changed in python-fixtures: | |
status: | New → Triaged |
importance: | Undecided → Medium |
Changed in python-fixtures: | |
assignee: | nobody → John L. Villalovos (happycamp) |
milestone: | none → next |
status: | Triaged → Fix Released |
This would be lovely to have.
https:/ /docs.python. org/2/library/ logging. html#logging. Formatter. format covers the bit in the logging glue where formatting actually happens.
So users can today use a customer formatter already, with a format() method that can do arbitrary stuff (e.g. journal errors to a list , and then have a cleanup call that asserts the list is empty.
Getting at the formatter instance will require a little poking around, and we could certainly make that easier in FakeLogger.
Making this work by default would be great too - for that I think we need:
- a local subclass for Formatter
- journal the exceptions to the fixture
- add a cleanUp that checks the exceptions list is empty
assertEqual isn't available outside of TestCase, but we can use the Equals matcher directly to format the error nicely and then just raise AssertionError(that string)