I am using the logger module of python (python version 3.x, but it should not matter) and I noticed that an error in a format string is reported as such:
Traceback (most recent call last):
File "/usr/lib/python3.1/logging/__init__.py", line 770, in emit
msg = self.format(record)
File "/usr/lib/python3.1/logging/__init__.py", line 650, in format
return fmt.format(record)
File "/usr/lib/python3.1/logging/__init__.py", line 438, in format
record.message = record.getMessage()
File "/usr/lib/python3.1/logging/__init__.py", line 308, in getMessage
msg = msg % self.args
TypeError: %d format: a number is required, not str
As you can see, there is no mention of where the actual error (in my code) was. By the way, here’s what was wrong in my code:
logging.debug('This is a string %d', str(foo))
Changing the %d in %s solved the problem.
My question is: how do I get some slightly more useful information from the logging module output? Do I have to write my own logger? Where do I tweak the logger module?
Was just about to post this when Unutbu beat me to it. But here it is anyway:
You could try subclassing whichever handler you’re using (example below using StreamHandler) and overiding the format method with a function that wraps it in a try: block
gives me:
I wouldn’t leave this in any production code, but it should help you find things like