views:

155

answers:

1

Hello, I want to save all following Exceptions in a file. The reason why I need this is because the IDLE for python 3.1.1 in Ubuntu raises an Exception at calltipps, but close to fast, that it isn't readble. Also I need this for testing. The best, would be if I just call a function which saves all Exception to a file. Thank you! ;)

// edit:

i had looked first for a more general way! so that you do not have to place your whole code in a function or indentation. but now that worked wery well for me. although I would be still grateful, if you find a way!

thanks!

+7  A: 

If you have a convenient main() function (whatever it's called), then you can use the logging module:

import logging

def main():
    raise Exception("Hey!")

logging.basicConfig(level=logging.DEBUG, filename='/tmp/myapp.log')

try:
    main()
except:
    logging.exception("Oops:")

logging.exception conveniently gets the current exception and puts the details in the log:

ERROR:root:Oops:
Traceback (most recent call last):
  File "C:\foo\foo.py", line 9, in <module>
    main()
  File "C:\foo\foo.py", line 4, in main
    raise Exception("Hey!")
Exception: Hey!
Ned Batchelder