如何仅打印文件/tmp/exceptions.log中存在的异常using python
,忽略所有调试语句。异常的快照如下所示。
Traceback (most recent call last):
File "/usr/site/bank/views.py", line 1695, in importmydata
o.save()
File "/usr/site/cl/django/django/db/models/base.py", line 435, in save
self.save_base(using=using, force_insert=force_insert, force_update=force_update)
IntegrityError: (1048, "Column 'option_b' cannot be null")
2011-04-14 11:57:40,895 DEBUG In exception
2011-04-14 11:57:40,915 DEBUG No resource found
2011-04-14 11:57:40,926 DEBUG Name
2011-04-14 11:57:40,915 DEBUG No resource found
2011-04-14 11:57:40,915 DEBUG No resource found
2011-04-14 11:57:40,915 DEBUG No resource found
2011-04-14 11:57:40,915 DEBUG No resource found
Traceback (most recent call last):
File "/usr/site/bank/views.py", line 1695, in importmydata
o.save()
File "/usr/site/cl/django/django/db/models/base.py", line 435, in save
self.save_base(using=using, force_insert=force_insert, force_update=force_update)
IntegrityError: (1048, "Column 'option_b' cannot be null")
2011-04-14 11:57:40,895 DEBUG In exception
2011-04-14 11:57:40,915 DEBUG No resource found
2011-04-14 11:57:40,926 DEBUG Name
答案 0 :(得分:1)
最简单的方法是使用带有排除标志的grep,例如
grep -v DEBUG /tmp/exceptions.log
这将打印不包含“DEBUG”字符串的行。
答案 1 :(得分:0)
>>> import re
>>> pat = re.compile(r'(\w+:\s.*\n)')
>>> pat.findall(c) # c is your logs content
['IntegrityError: (1048, "Column \'option_b\' cannot be null")\n', 'IntegrityError: (1048, "Column \'option_b\' cannot be null")\n']
>>>
答案 2 :(得分:0)
你也可以使用PyPI中提供的“logview”。如果您查看我的PyWorkbooks代码的最新版本(此处为https://sourceforge.net/projects/pyworkbooks/),则会有一个easy_log文件,用于设置日志视图应用程序的日志记录。
logview是一个优秀的gui,允许你做过滤输入等事情。
答案 3 :(得分:0)
如果您使用的是try-except结构,则可以执行以下操作:
try:
foo()
except Exception,ex:
print "Exception %s"%str(ex) # Output exception
print "Unexpected error:", sys.exc_info()[0] # Exception message
import traceback
traceback.print_exc() # Output full traceback if you want one