我将记录设置如下
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)
答案 0 :(得分:1)
您应该在日志消息中添加%s值,因此它会是这样的:
logger.debug('content of file not propper: %s', ie)
我也以不同的方式使用了记录器,只是直接添加消息:
logger.debug('content of file not propper: %s' % ie)
在我看来更直接的前进。
关于pdb,这是一个非常好的调试程序的方法,我建议100%:)