python: convey the exception traceback into log file
February 27th, 2008 mysurface Posted in Developer, python | Hits: 44475 | 6 Comments »
Python is the interpreter language, you do not need to compile your code, and also you have no ways to check for your syntax error until you run your python script. Either syntax error or runtime error will be throw to standard output through python exception handler by default.
Python throw the exception with traceback info indicate that which part of your scripts has gone wrong, it looks like this:
Traceback (most recent call last):
File "except.py", line 9, in
main()
File "except.py", line 5, in main
print l[4]
IndexError: list index out of range
The traceback indicate me that there is an IndexError happen in my main function. This piece of good info are very helpful for me to do the debugging. But we might use python for some automated jobs where I runs my python scripts with crontab. So its hard for me to discover my bugs with the traceback throw to the screen.
So this question comes to my head.
” Can I log the traceback into a text file and let the python script ends peacefully?”
First of all, I thought I can get it from Exception class, but the fact is I can only get it from traceback class, check out the sample here:
#!/usr/bin/env python
import sys, traceback
def main():
l=[1,2,3,4]
print l[4]
if __name__=="__main__":
try:
main()
except:
print "Trigger Exception, traceback info forward to log file."
traceback.print_exc(file=open("errlog.txt","a"))
sys.exit(1)
By using traceback.print_exc() I can now append the exception traceback to my log file.







February 27th, 2008 at 10:42 pm
i think it,s a great post.
April 17th, 2008 at 4:11 pm
I use internet through Mobile. I am using bluetooth device to connect my mobile and pc. I have been using Microsoft and Fedora 7 linux. Linux installed in another partition. I want to surf internet in Linux. But, I can’t install Bluetooth software in Linux. Can anyone help me to resolve this problem?
September 15th, 2008 at 10:41 am
Hey, this is great information. Thanks for sharing it!
February 5th, 2009 at 1:43 am
[...] python: convey the exception traceback into log file Python is the interpreter language, you do not need to compile your code, and also you have no ways to check for your sy… [...]
November 21st, 2009 at 8:31 am
Good one, but here is another one with the use of python logging. It’s required a bit more code.. (well in my case it’s about 6 lines more :)
import logging
…
#set your logger in __init__ or so, check out logging how to do
setLogger(logging.INFO, “app.log”)
log = logging.getLogger(“myclass”)
…
try:
…
except Exception:
log.error(“Crash”, exc_info=1)
finally:
#be nice
This will log the text and then the trace log by your logger. Depend on how you set your logger, it could end up in multiple files, in console in db.. whatever you want to. In my case, it’s end up on the console (in debugger) and in my log file as:
11-21 01:10 myclass ERROR Crash
Traceback (most recent call last):
File “/mycodepath/myclass.py”, line 84, in mainLoop
print l[2]
IndexError: list index out of range
if you click on the line in the debugger (eg. eclipse) you can get to the location of the crash..
August 3rd, 2011 at 4:10 pm
Thanks for the post, v handy!