logging - string formating problems

Steven D'Aprano steven at REMOVE.THIS.cybersource.com.au
Mon Apr 6 12:29:01 CEST 2009

On Mon, 06 Apr 2009 11:11:37 +0200, Werner F. Bruhin wrote:

> Werner F. Bruhin wrote:
>> I see the following exception with a string formating problem.
>> TypeError: not all arguments converted during string formatting
>> Traceback (most recent call last):
>>  File "/usr/lib/python2.5/logging/__init__.py", line 744, in emit
>>    msg = self.format(record)
>>  File "/usr/lib/python2.5/logging/__init__.py", line 630, in format
>>    return fmt.format(record)
>>  File "/usr/lib/python2.5/logging/__init__.py", line 418, in format
>>    record.message = record.getMessage()
>>  File "/usr/lib/python2.5/logging/__init__.py", line 288, in getMessage
>>    msg = msg % self.args
>> The exception does not give any information on where the problem is
>> coming from.

I'm pretty sure it does. Are you sure that you are including the *entire* 
traceback? Because judging from the four entries given, it looks like the 
error is originating from logging.__init__ itself, and not from your 
application itself. Weird huh? 

Oh wait... is this error occurring when you exit your application? 
Possibly this is the logging shutdown handler failing? I'm kind of 
whistling in the dark here.

Perhaps this recipe can help you get more information from the tracebacks:


Good luck, and let us know what the problem was.

>> I am using Python 2.5.4 but I see that in 2.6 the code is still the
>> same.
>> Any chance that getMessage could catch this exception and provide
>> better debugging information (i.e. content of msg and self.args).


You could always request a feature enhancement on the Python bug list.

> I understand that my problem is that the arguments don't match the
> format.  But currently the traceback is not of any help in figuring out
> where this in my code this is.
> So, I suggest that line 288 in getMessage is changed from:
>         msg = msg % self.args
> To something along these lines:
>         if self.args:
>             try:
>                 msg = msg % self.args
>             except:
>                 print 'msg: %s' % msg
>                 print 'args: %s' % self.args
>                 traceback.print_exception(ei[0], ei[1], ei[2])
>         return msg

Patches will increase the likelihood of the enhancement being accepted, 
but I suspect this is too *specific* to be accepted.


More information about the Python-list mailing list