Capturing exceptions?

Robert Brewer fumanchu at amor.org
Wed Aug 25 13:15:50 EDT 2004


Dfenestr8 wrote:
> What's the easiest way to capture the traceback from an exception, and
> write it to a file? Ideally, I'd like to be able to do something like:
> 
> >try:
> >	main()
> >except:
> >	write the traceback to a file

Have a look at the logging module:
http://docs.python.org/lib/module-logging.html. Loggers can be a bit
complex at first to set up; read the example in the docs
(http://docs.python.org/lib/node301.html) and go from there.

The upshot is they lead to extremely simple traceback logging:

try:
    shutdown()
except Exception:
    logger.exception("SHUTDOWN")

This snippet writes the full traceback to the file.

HTH,


Robert Brewer
MIS
Amor Ministries
fumanchu at amor.org



More information about the Python-list mailing list