How do I log exceptions that happen in my app? I need to have a log file...
Cheers,
Ali Shafai
How do I log exceptions that happen in my app? I need to have a log file...
Cheers,
Ali Shafai
hi alishafa
have look at the wiki recently there are lot of articles on this.
hope this helps give a feedback.
Gargi Das- http://gargidas.blogsot.com
Forum Nokia Python Wiki
Learn Python at http://mobapps.org/PyS60
Code:import sys, traceback, codecs #Open the log file f=codecs.open("C:\\Python\\exceptionlog.txt", "w", "utf_8") #When an exception occurs, print it in the file #For example: try:print 3/0 except:f.write(unicode(traceback.format_exception(*sys.exc_info()))) #After you are done with the file, close it f.close()
Last edited by bogdan.galiceanu; 2008-04-17 at 12:45.
Well, I do it all the time (using Windows Vista and ConText text editor).
File does not contain all info due internal buffering, that's true, but situation can be improved by using "f.flush()". Either imemdiately after each "print/write" or only in some places, for example when you know that app will pause to wait user input.
Closing the file will flush the internal output buffer completely and you get the rest of file content, too.
Cheers,
--jouni
Thanks, it works! but not exactly the way I wanted. I was looking for a central place to catch exceptions:
Class MyApplication:
def run(self):
do something that can throw an exception
.
.
.
try:
myApplication = MyApplication()
myApplication.run()
except:
LogTheException()
but, what happens is I have to put the try/catch in my run method or it will not catch the exception.
any help appreciated.
Cheers,
Ali