开发者

How do I stop 'print' from outputting to the browser with Google App Engine?

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):开发者_如何学JAVA
    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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜