Forums

Flask: How to throw error messages?

In the interactive part of my app which I cannot easily execute in the shell (or how can I?), how do I get error messages?

I can return my own error messages and see them in the browser, but I would like to get the interpreter's error descriptions that I would see in the shell.

sys.exc_info()[0] does not work.

If you print messages to sys.stderr then they'll appear in your error logs (which you can view from the Web tab). So, for example

import sys

...

print >> sys.stderr, "Debug message here"

ohhhh, i didn't know that. i always created a message function, which output debugging messages to a file

Yep. If you're using Python 3, the syntax is slightly different:

print("Debug message here", file=sys.stderr)

Thanks but this still does not solve the core of my problem:

I would like to see WHAT caused the exception, like for example:

'list index out of range' ...

Hmm. Perhaps the best way would be to use the Python logging module. You can set it up to use stderr like this:

import logging
import sys

...

logging.basicConfig(stream=sys.stderr, level=logging.DEBUG)

Then, when you want to log a full exception, you just do this in the except block:

try:
    # do stuff
except:
    logging.exception("Error when doing something")

This will not only print out the error message you specify, but because it's in an except block, it will log the complete stack trace including the exception's message.

Is there any way to do this as a default? Particularly in django. Or would that need to be imported and set on every file?

If you put the imports and the basicConfig in the settings.py file, that should set everything up properly.

Does this still work?

I just tried it adding it to my settings.py, reloaded the project, but my debug log messages are not showing up in my error log.

printing direct to sys.stderr works....

Hi Halem,

in django, there is logging functionality built in like harry said. Go to settings.py and add the LOGGING constant- set it up like this. These should print to your access.log and not error.log

Also, I just want to check that you know that django has a debug=True option (which should be set by default), which will usually print the error message directly on the url that you are accessing- meaning that you don't need to do any of this logging for more simplistic errors.

Conrad

@Conrad,

Sorry, I'm actually rcooke. I keep forgetting my "name" is changed when I'm logged in to a cilent's account.

Yes, I am familiar with the debug flag. However, I'm using the error log to dump diagnostic messages and repr's of stuff I want to dissect.

Ironically, I used the LOGGING = construct for a previous project, I just thought Gile's one-liner was a handy looking shortcut.

Guess not!

It would be handy if you added some specific purpose LOGGING = setups for handy things, like:

- Everything
- All SQL traffic
- Just a user's debug or info messages.

Just to save us (me) the time it takes to figure it out.....

Yeah, django's logging config is a pain isn't it? And the Python logging module in general is way too enterprisey. I wrote some half-formed thought about it here: http://www.obeythetestinggoat.com/how-to-log-exceptions-to-stderr-in-django.html. Warning -- using the root logger for this stuff is bad practice (TM).

Will have a think about whether it's something we could help users with somehow...