logging.getLogger(__name__) >>> >>> try: ... foo = bar ... except: ... log.exception("Something went wrong") ... # or: log.error("Uh oh", exc_info=True) ... ERROR:__main__:Something went wrong Traceback (most recent call last): File "<stdin>", line 2, in <module> NameError: name 'bar' is not defined
... print "Got an exception!" ... print type, value, traceback ... ... >>> sys.excepthook = my_excepthook >>> >>> foo = bar Got an exception! <type 'exceptions.NameError'> name 'bar' is not defined <traceback object at 0x10d619950>
... print "trying..." ... foo = bar ... except: ... print "excepting..." ... bar = baz ... finally: ... print "finally..." ... return ... >>> main() trying... excepting... finally... Traceback (most recent call last): File "<stdin>", line 1, in <module> File "<stdin>", line 7, in main NameError: global name 'baz' is not defined