How to log python exception ?

Posted by Maxim Veksler on Stack Overflow See other posts from Stack Overflow or by Maxim Veksler
Published on 2010-12-22T11:46:37Z Indexed on 2010/12/22 11:54 UTC
Read the original article Hit count: 353

Filed under:
|
|

Hi,

Coming from java, being familiar with logback I used to do

try { 
...
catch (Exception e) { 
   log("Error at X", e);
}

I would like the same functionality of being able to log the exception and the stacktrace into a file.

How would you recommend me implementing this?
Currently using boto logging infrastructre, boto.log.info(...)

I've looked at some options and found out I can access the actual exception details using this code:

import sys

try:
    1/0
except:
    exc_type, exc_value, exc_traceback = sys.exc_info()
    traceback.print_exception(exc_type, exc_value, exc_traceback)

I would like to somehow get the string print_exception() throws to stdout so that I can log it.

Thank you,
Maxim.

© Stack Overflow or respective owner

Related posts about python

Related posts about exception-handling