logging - string formating problems

Vinay Sajip vinay_sajip at yahoo.co.uk
Mon Apr 6 20:35:08 CEST 2009

On Apr 6, 1:58 pm, "Werner F. Bruhin" <werner.bru... at free.fr> wrote:
> I am fully aware that the problem is in my code, however as getMessage
> inlogging.__init__.py does not catch the exception it is pretty
> difficult to find the problem without manually inspecting anylogging.something statements.
> My hack oflogging.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 hacklogging.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.
> Werner

Handlers should direct any exceptions raised during emit() to
handleError(), except for SystemExit and KeyboardInterrupt which are
re-raised. So, you should be able to redefine handleError to print the
relevant information if a TypeError is raised, either by subclassing
or by monkey-patching as Peter Otten has suggested. However,
handleError normally swallows the exceptions if raiseExceptions is
false (by default, it's set to true. It's meant to be set to false
only in production environments where exceptions raised in logging
should have no influence on the application's behaviour).


Vinay Sajip

More information about the Python-list mailing list