views:

29

answers:

2

I'm new to GAE, and have not been able to figure out how to configure 'print' statements to the logging console rather than the browser. For example:

class Feed(webapp.RequestHandler):
    def post(self):
        feeditem = Feeditem()
        feeditem.author = self.request.get('from')
        feeditem.content = self.request.get('content')
        feeditem.put()
        notify_friends(feeditem)
        self.redirect('/')

def notify_friends(feeditem):
    """Alerts friends of a new feeditem"""    
    print 'Feeditem = ', feeditem

When I do something like the above, the print in notify_friends outputs to the browser and somehow prevents the self.redirect('/') in the post method that called it. Commenting it out corrects the issue.

Is there a way to change this behavior?

EDIT: Google App Engine tag removed as this is general.

+2  A: 

This is not just a problem with GAE. It is a general issue. You can't print out HTML and then try to have a redirect header. The answer to your question is no, you can't change the behavior. What exactly are you trying to achieve? You might be able to get what you want a different way.

Aaron D
I'm just trying to use the prints to debug as I go. Is there an alternative that does not prevent the use of a redirect header?
Joe
Use logging as indicated in the other answer
Aaron D
+3  A: 

You should instead use the logging module, like so:

import logging
def notify_friends(feeditem):
    """Alerts friends of a new feeditem"""    
    logging.info('Feeditem = %s', feeditem)

There are a variety of logging levels you can use, from debug to critical. By default, though, the App Engine SDK only shows you log messages at level info and above, so that's what I've suggested here. You can ask it to show you debug messages if you want, but you'll probably be overwhelmed with useless (to you) logging information, at least when running in the SDK.

See the logging module docs for more info.

Oh, and the nice thing about using the logging module is that you'll have access to your log messages in production, under the "Logs" section of your app's the App Engine dashboard.

Will McCutchen
Perfect - thanks.
Joe