2

I set the logging as below

import logging

logging.basicConfig(
    level = logging.DEBUG,
    format = '%(asctime)s %(levelname)s %(message)s',
)

logger = logging.getLogger('myapp.views')

def my_view(request):
    ...
    try:
        parse_file_using_regex(file):
        ...
    except IndexError as ie:
        logger.debug('content of file not proper:',ie)
    except ValueError as ve:
        logger.debug('caused value error')

When a valuError occurs ,I get the following output

...Traceback (most recent call last):
  File "/usr/lib/python2.6/logging/__init__.py", line 768, in emit
    msg = self.format(record)
  File "/usr/lib/python2.6/logging/__init__.py", line 648, in format
    return fmt.format(record)
  File "/usr/lib/python2.6/logging/__init__.py", line 436, in format
    record.message = record.getMessage()
  File "/usr/lib/python2.6/logging/__init__.py", line 306, in getMessage
    msg = msg % self.args
TypeError: not all arguments converted during string formatting
....Traceback (most recent call last):
  File "/usr/lib/python2.6/logging/__init__.py", line 768, in emit
    msg = self.format(record)
  File "/usr/lib/python2.6/logging/__init__.py", line 648, in format
    return fmt.format(record)
  File "/usr/lib/python2.6/logging/__init__.py", line 436, in format
    record.message = record.getMessage()
  File "/usr/lib/python2.6/logging/__init__.py", line 306, in getMessage
    msg = msg % self.args
TypeError: not all arguments converted during string formatting
2012-06-21 09:13:47,909 DEBUG caused value error

Can somebody help me figure this out? Why do I get a TypeError?

using pdb,and including the following in getMessage(self) of python2.6/logging/init.py

import pdb
pdb.set_trace()
print msg,sef.args

...> /usr/lib/python2.6/logging/__init__.py(307)getMessage()
-> print msg,sef.args
(Pdb) msg
'parsing subtitle file:'
(Pdb) self.args
('/home/me/dev/python/django/myapp/media/testpath/testfile.srt',)
(Pdb) 
2
  • could you print the msg and self.args values? just go to the "/usr/lib/python2.6/logging/__init__.py" file, write "import pdb; pdb.set_trace(); print msg, self.args" in line 305 and run it again Commented Jun 21, 2012 at 5:46
  • I have updated the question with the results of the above Commented Jun 21, 2012 at 10:19

1 Answer 1

1

You should add to your logging message a %s value, so it would be like this:

logger.debug('content of file not propper: %s', ie)

I have used the logger in a different way also, just adding the message directly like this:

logger.debug('content of file not propper: %s' % ie)

which seems to me a lot more straight foward.

About the pdb, it is a really nice way to debug your program, I recommend it 100% :)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.