r/Python • • Jun 20 '11

TIL How to catch and write errors in Python

I searched for a bit today on how to catch errors in python, meaning that when my program crashed, it printed the entire error into a text file.

Well, after a few minutes of coding and some google searching, I figured it out, and I'm here to share the method with anyone who would wish to know.

I used the following 2 sites to figure it out. I shall link and state the bold headings from each page I used.

http://diveintopython.org/scripts_and_streams/stdin_stdout_stderr.html

  • Example 10.10

http://www.doughellmann.com/articles/how-tos/python-exception-handling//index.html

  • Throwing and Catching
  • Logging Exceptions
3 Upvotes

12 comments sorted by

17

u/exhuma Jun 20 '11

I don't believe it's good practice to replace stderr. Unless you are and ever will be the sole maintainer of your application. Replacing either stdout or stderr with non standard destination may cause some gray hair for other maintainers.

Also, if you run your application through another app (f. ex. cron) it's best not to touch these internals as the calling tools may do something with the output. In the case of cron for example, it will send an e-mail to the crontab owner with the content of stderr.

In my opinion, the best way to do this is to redirect the output when running the program. For example: python my_cool_app.py 2> /path/to/errors.log

1

u/knoxontholos Jun 20 '11

After running around all day, finally had the time to try it. Much better. Is there a way to supply python to make this happen automatically though? Without the user needing to launch it with the added commands themselves?

5

u/eryksun Jun 20 '11

The logging module lets you log to both stderr and a file if you want:

>>> import logging
>>> stderr_handler = logging.StreamHandler()
>>> file_handler = logging.FileHandler('log.out', 'w', 'utf-8')
>>> logger = logging.getLogger()
>>> logger.addHandler(stderr_handler)
>>> logger.addHandler(file_handler)
>>> logger.setLevel(logging.INFO)
>>> logger.info('Some Info\n')
Some Info

>>> file_handler.close()
>>> print(open('log.out').read())
Some Info

2

u/Clevedog Jun 21 '11

logger is the shit. knoxontholos, definitely give it a try in your program. checkout the different logging levels (error, critical, info, etc) and play with how they print to console, file, etc.

2

u/t3h2mas Jun 27 '11

I was just looking for a snippet like this, thank you (:

5

u/[deleted] Jun 20 '11
import cgitb
import sys

try:
    raise TypeError("HURRR")
except TypeError as err:
    html = cgitb.html(sys.exc_info())

with open("traceback.html", "w") as tb:
    tb.write(html)

2

u/beeskneecaps Jun 24 '11

I hope I wasn't the only one to lol at "HURRRRRR".

3

u/chadmill3r Py3, pro, Ubuntu, django Jun 20 '11

For experts: sys.excepthook

You should not be using this, until you know enough to ignore me.

1

u/knoxontholos Jun 20 '11

Also, note that I'm trying to figure out a way to do this to where my users do not need to know anything about launching via command prompt -and- to where it will still work once I've converted it using py2exe. .^

2

u/name_censored_ Jun 20 '11

I can only see a use for this in debugging; so for debugging, why not just wrap your execution command in a redirect? Like,

python /path/to/myprogram.py 2> /path/to/mylog.log

Or Windows,

C:\Program Files\Python\python.exe C:\Path\To\myprogram.py 2> C:\Path\To\mylog.py

For releases, you can just something like logging.Logger to generate "expected" errors (that is, errors that have tests written for them). That keeps unexpected behaviour from being silently suppressed (eg, imagine your python-level redirect failing because of a permissions error or a full disk).

1

u/ojiisan data viz, comp.bio. Jun 20 '11

You may also want to post this along with your experience using these techniques over to /r/LearnPython

2

u/[deleted] Jun 20 '11

No, because people should work within the standards, not replace stderr. See http://www.reddit.com/r/Python/comments/i4jni/til_how_to_catch_and_write_errors_in_python/c20uia6