Writing traceback of an exception into a log-file
Gabriel Genellina
gagsl-py2 at yahoo.com.ar
Mon May 26 11:52:25 EDT 2008
En Mon, 26 May 2008 05:31:27 -0300, <Dominique.Holzwarth at ch.delarue.com> escribió:
> I'm trying to write the traceback stack of an exception that is thrown into a log file, but currently all that's written is simply a "None" :-/
>
> Here's some example code of what I'm doing:
>
> Import traceback
> class myException(Exception):
> def __init__(self):
> f = open('filename.txt', 'a')
> traceback.print_exc(file=f)
> f.close()
>
> The file handling (opening etc) works ok (I'm using the same file in combination with a Logger() object too) but the string written to the file is effectively just a "None".
>
> Could anyone explain to me how to properly use the traceback module with it's countless of functions? =)
Don't inherit from Exception - you should be able to log *any* exception, not only this specific one, I presume? print_exc writes "the exception currently being handled", not the one you're creating right now. Put the code above into your exception handler:
try:
1/0
except:
f = open('filename.txt', 'a')
traceback.print_exc(file=f)
f.close()
(or define a function and call it from the exception handler)
--
Gabriel Genellina
More information about the Python-list
mailing list