FAQ
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 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).

Werner

Search Discussions

  • MRAB at Apr 5, 2009 at 5:49 pm

    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 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).
    The logging methods such as .debug(), etc, look like this:

    logging.debug(msg[, *args[, **kwargs]])

    where msg is actually a format string and args are the arguments
    (values) for that format.

    Your problem is that somewhere the arguments don't match the format.
  • Werner F. Bruhin at Apr 6, 2009 at 9:11 am

    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 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).
    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

    Werner
  • Steven D'Aprano at Apr 6, 2009 at 10:29 am

    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:

    http://code.activestate.com/recipes/52215/

    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).
    *shrug*

    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.



    --
    Steven
  • Peter Otten at Apr 6, 2009 at 11:31 am

    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 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).
    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
    I would be more interested in the origin of the problem in my code. For that
    you need the complete traceback. You might be able to find it by
    monkey-patching the Handler.handlError() method, e. g.:

    import logging

    def handleError(self, record):
    raise

    logging.Handler.handleError = handleError

    logging.critical("", 42)

    Of course you'd only do that while you are debugging the app.

    Peter
  • Werner F. Bruhin at Apr 6, 2009 at 12:58 pm
    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.

    Werner
  • MRAB at Apr 6, 2009 at 1:34 pm

    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!)
  • Vinay Sajip at Apr 6, 2009 at 6:35 pm

    On Apr 6, 1:58?pm, "Werner F. Bruhin" 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).

    Regards,

    Vinay Sajip

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedApr 5, '09 at 5:29p
activeApr 6, '09 at 6:35p
posts8
users5
websitepython.org

People

Translate

site design / logo © 2022 Grokbase