logging - string formating problems
MRAB
google at mrabarnett.plus.com
Mon Apr 6 09:34:13 EDT 2009
Werner F. Bruhin wrote:
> I am fully aware that the problem is in my code, however as getMessage
> in logging.__init__.py does not catch the exception it is pretty
> difficult to find the problem without manually inspecting any
> logging.something statements.
>
> My hack of logging.py is really a hack and I know that this can not be
> used a patch but I hope that someone with more know how then me knows
> how to improve the situation.
>
> BTW, using the below hack was enough for me to mind the bad code in my
> stuff, but it shouldn't have been this difficult.
>
> Traceback (most recent call last):
> File "C:\Python25\lib\logging\__init__.py", line 750, in emit
> msg = self.format(record)
> File "C:\Python25\lib\logging\__init__.py", line 636, in format
> return fmt.format(record)
> File "C:\Python25\lib\logging\__init__.py", line 424, in format
> record.message = record.getMessage()
> File "C:\Python25\lib\logging\__init__.py", line 288, in getMessage
> msg = msg % self.args
> TypeError: not all arguments converted during string formatting
>
>
> Then I hack logging.py and change line 288 in getMessage from:
> msg = msg % self.args
>
> to:
> try:
> msg = msg % self.args
> except:
> print 'msg: %s' % msg
> print 'args: %s' % self.args
> print traceback.format_exc()
>
> I get the following which helps a lot more in finding were in my code I
> have the problem:
>
> msg: ping_min: 1
> args: replace
> Traceback (most recent call last):
> File "C:\Python25\lib\logging\__init__.py", line 290, in getMessage
> msg = msg % self.args
> TypeError: not all arguments converted during string formatting
>
>
> As mentioned above, I know the above code is not good enough at all, but
> I hope that maybe Vinay Sajip or someone else with more know how then I
> have can come up with a patch which will make this type of situation
> easier.
>
It looks like what you need is for the module to record the exception in
the log you're generating as a "logging error" or some such. BTW, you
should catch only TypeError and not use an empty "except":
try:
msg = msg % self.args
except TypeError:
msg = 'Logging error: msg is %s, args is %s' % (repr(msg),
repr(self.args))
msg += traceback.format_exc()
(needs tidying up, of course!)
More information about the Python-list
mailing list