如何把報錯內容存入資料庫

1 view
Skip to first unread message

SeedSeek

unread,
Feb 14, 2017, 10:59:49 PM2/14/17
to Uliweb
程式執行一半如果報錯,要如何將整個 python 報錯的內容存到資料庫?

目前用 try except + traceback.format_exc()
但用try 時 orm.Rollback 會不正常

有什麼好方法可以解決?

limodou

unread,
Feb 15, 2017, 8:24:44 AM2/15/17
to uliweb
可以考虑写一个middleware,如:

cat traceback_middleware/middle_traceback.py

#coding=utf8
from uliweb import Middleware
import sys

class TracebackMiddle(Middleware):
ORDER = 40

def process_exception(self, request, e):
from uliweb import settings, error,functions
import traceback
from uliweb.utils.common import log
from uliweb.core.SimpleFrame import HTTPError, HTTPException, NotFound

if isinstance(e, (HTTPError, NotFound, HTTPException)):
return

type, value, tb = sys.exc_info()
txt = ''.join(traceback.format_exception(type, value, tb))

log.exception(e)

#将txt写入数据库即可


然后在settings.ini中配置为:

[MIDDLEWARES]
traceback = 'traceback_middleware.middle_traceback.TracebackMiddle'
> --
> -- ----
> Project : https://github.com/limodou/uliweb
> doc : http://limodou.github.com/uliweb-doc
> ---
> You received this message because you are subscribed to the Google Groups
> "Uliweb" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to uliweb+un...@googlegroups.com.
> To post to this group, send email to uli...@googlegroups.com.
> Visit this group at https://groups.google.com/group/uliweb.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/uliweb/c27f4b52-3292-4c30-8ce1-99a2c6674795%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.



--
I like python!
UliPad <<The Python Editor>>: http://code.google.com/p/ulipad/
UliWeb <<simple web framework>>: https://github.com/limodou/uliweb
My Blog: http://my.oschina.net/limodou

Chunlin Zhang

unread,
Feb 15, 2017, 10:18:25 AM2/15/17
to uli...@googlegroups.com
好赞,又学了一招


> To post to this group, send email to uli...@googlegroups.com.
> Visit this group at https://groups.google.com/group/uliweb.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/uliweb/c27f4b52-3292-4c30-8ce1-99a2c6674795%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.



--
I like python!
UliPad <<The Python Editor>>: http://code.google.com/p/ulipad/
UliWeb <<simple web framework>>: https://github.com/limodou/uliweb
My Blog: http://my.oschina.net/limodou
--
-- ----
Project : https://github.com/limodou/uliweb
doc : http://limodou.github.com/uliweb-doc
---
You received this message because you are subscribed to the Google Groups "Uliweb" group.
To unsubscribe from this group and stop receiving emails from it, send an email to uliweb+unsubscribe@googlegroups.com.

To post to this group, send email to uli...@googlegroups.com.
Visit this group at https://groups.google.com/group/uliweb.

cnidance

unread,
Feb 18, 2017, 2:56:36 AM2/18/17
to uli...@googlegroups.com
使用 traceback_middleware/middle_traceback.py 將錯誤的內容寫入資料庫後還會再被Rollback ,有方法解決?


> To post to this group, send email to uli...@googlegroups.com.
> Visit this group at https://groups.google.com/group/uliweb.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/uliweb/c27f4b52-3292-4c30-8ce1-99a2c6674795%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.



--
I like python!
UliPad <<The Python Editor>>: http://code.google.com/p/ulipad/
UliWeb <<simple web framework>>: https://github.com/limodou/uliweb
My Blog: http://my.oschina.net/limodou
--
-- ----
Project : https://github.com/limodou/uliweb
doc : http://limodou.github.com/uliweb-doc
---
You received this message because you are subscribed to the Google Groups "Uliweb" group.
To unsubscribe from this group and stop receiving emails from it, send an email to uliweb+unsubscribe@googlegroups.com.

To post to this group, send email to uli...@googlegroups.com.
Visit this group at https://groups.google.com/group/uliweb.

For more options, visit https://groups.google.com/d/optout.



--
~Ten ~

limodou

unread,
Feb 18, 2017, 9:22:03 AM2/18/17
to uliweb
那可以考虑使用象Redis之类的先进行缓存,然后再通过另一个进程读redis来保存数据库。
>> > email to uliweb+un...@googlegroups.com.
>> > To post to this group, send email to uli...@googlegroups.com.
>> > Visit this group at https://groups.google.com/group/uliweb.
>> > To view this discussion on the web visit
>> >
>> > https://groups.google.com/d/msgid/uliweb/c27f4b52-3292-4c30-8ce1-99a2c6674795%40googlegroups.com.
>> > For more options, visit https://groups.google.com/d/optout.
>>
>>
>>
>> --
>> I like python!
>> UliPad <<The Python Editor>>: http://code.google.com/p/ulipad/
>> UliWeb <<simple web framework>>: https://github.com/limodou/uliweb
>> My Blog: http://my.oschina.net/limodou
>>
>> --
>> -- ----
>> Project : https://github.com/limodou/uliweb
>> doc : http://limodou.github.com/uliweb-doc
>> ---
>> You received this message because you are subscribed to the Google Groups
>> "Uliweb" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to uliweb+un...@googlegroups.com.
>> To post to this group, send email to uli...@googlegroups.com.
>> Visit this group at https://groups.google.com/group/uliweb.
>> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/uliweb/CADCtg9-ZhwcF%2BA-Xza-X%2BnT1DYs9%3D06tmayfVV1EUHXVdN4P9g%40mail.gmail.com.
>> For more options, visit https://groups.google.com/d/optout.
>
>
>
>
> --
> ~Ten ~
>
> --
> -- ----
> Project : https://github.com/limodou/uliweb
> doc : http://limodou.github.com/uliweb-doc
> ---
> You received this message because you are subscribed to the Google Groups
> "Uliweb" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to uliweb+un...@googlegroups.com.
> To post to this group, send email to uli...@googlegroups.com.
> Visit this group at https://groups.google.com/group/uliweb.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/uliweb/CAHLXMV988CkwpYpzNhRORYW_ws4L_GWnZHPhg64s%3DxWqeUYJjg%40mail.gmail.com.

Chunlin Zhang

unread,
Feb 18, 2017, 10:12:07 AM2/18/17
to uli...@googlegroups.com
那如果没有redis,可以输出到特别目录里的文件,然后专门有脚本将文件内容另外导进数据库里,这样也简单

那可以考虑使用象Redis之类的先进行缓存,然后再通过另一个进程读redis来保存数据库。

>> > To post to this group, send email to uli...@googlegroups.com.
>> > Visit this group at https://groups.google.com/group/uliweb.
>> > To view this discussion on the web visit
>> >
>> > https://groups.google.com/d/msgid/uliweb/c27f4b52-3292-4c30-8ce1-99a2c6674795%40googlegroups.com.
>> > For more options, visit https://groups.google.com/d/optout.
>>
>>
>>
>> --
>> I like python!
>> UliPad <<The Python Editor>>: http://code.google.com/p/ulipad/
>> UliWeb <<simple web framework>>: https://github.com/limodou/uliweb
>> My Blog: http://my.oschina.net/limodou
>>
>> --
>> -- ----
>> Project : https://github.com/limodou/uliweb
>> doc : http://limodou.github.com/uliweb-doc
>> ---
>> You received this message because you are subscribed to the Google Groups
>> "Uliweb" group.
>> To unsubscribe from this group and stop receiving emails from it, send an

>> To post to this group, send email to uli...@googlegroups.com.
>> Visit this group at https://groups.google.com/group/uliweb.
>> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/uliweb/CADCtg9-ZhwcF%2BA-Xza-X%2BnT1DYs9%3D06tmayfVV1EUHXVdN4P9g%40mail.gmail.com.
>> For more options, visit https://groups.google.com/d/optout.
>
>
>
>
> --
> ~Ten ~
>
> --
> -- ----
> Project : https://github.com/limodou/uliweb
> doc : http://limodou.github.com/uliweb-doc
> ---
> You received this message because you are subscribed to the Google Groups
> "Uliweb" group.
> To unsubscribe from this group and stop receiving emails from it, send an

> To post to this group, send email to uli...@googlegroups.com.
> Visit this group at https://groups.google.com/group/uliweb.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/uliweb/CAHLXMV988CkwpYpzNhRORYW_ws4L_GWnZHPhg64s%3DxWqeUYJjg%40mail.gmail.com.
>
> For more options, visit https://groups.google.com/d/optout.



--
I like python!
UliPad <<The Python Editor>>: http://code.google.com/p/ulipad/
UliWeb <<simple web framework>>: https://github.com/limodou/uliweb
My Blog: http://my.oschina.net/limodou

--
-- ----
Project : https://github.com/limodou/uliweb
doc : http://limodou.github.com/uliweb-doc
---
You received this message because you are subscribed to the Google Groups "Uliweb" group.
To unsubscribe from this group and stop receiving emails from it, send an email to uliweb+unsubscribe@googlegroups.com.

To post to this group, send email to uli...@googlegroups.com.
Visit this group at https://groups.google.com/group/uliweb.
Reply all
Reply to author
Forward
0 new messages