我将日志记录设置如下
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')
当发生 valuError 时,我得到以下输出
...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
有人可以帮我解决这个问题吗?为什么我会收到 TypeError?
使用 pdb,并在getMessage(self)
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)