Please note that the contents of this offline web site may be out of date. To access the most recent documentation visit the online version .
Note that links that point to online resources are green in color and will open in a new window.
We would love it if you could give us feedback about this material by filling this form (You have to be online to fill it)



Logging Events in Your Application

Marzia Niccolai, Google Developer Programs
April 2008

Introduction

Google App Engine logs events that take place in your application, which is accessible through the Logs API ( Python , Java , PHP , Go ). The logs that have been generated by your application are also viewable under that application in the Administration Console.

This article covers using the Python logging module. To familiarize yourself with how the Python logging module works, please see the module documentation at Python.org .

Logging Different Kinds of Events in your Application

The Python logging module allows a developer to log 5 levels of severity: Debug, Info, Warning, Error and Critical.

Below is an example on how one might use some of these levels to effectively log events in a simple Guestbook application:

import logging
import os
import datetime
import wsgiref.handlers

from google.appengine.api import users
from google.appengine.ext import db
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template

class Post(db.Model):
    author = db.StringProperty()
    content = db.StringProperty()
    date = db.DateTimeProperty()


class MainPage(webapp.RequestHandler):

  def get(self):
      # Retrieve existing posts from the datastore when getting the Main Page
      try:
          post_query = db.GqlQuery('SELECT * FROM Post ORDER BY date DESC')
          posts = [post for post in post_query]
      except:
          logging.error('There was an error retrieving posts from the datastore')

      template_values = {
          'posts': posts,
      }
      path = os.path.join(os.path.dirname(__file__), 'index.html')
      self.response.out.write(template.render(path, template_values))


class Guestbook(webapp.RequestHandler):
    # The user has posted a comment to the guest book
    def post(self):
        logging.debug('Start guestbook signing request')

        post = Post()

        if users.get_current_user():
            logging.info('Signed by user %s', users.get_current_user().nickname())
            post.author = users.get_current_user().user_id()
        else:
            logging.info('Signed by anonymous user')

        post.content = self.request.get('content')
        post.date = datetime.datetime.now()

        try:
            post.put()
        except:
            logging.error('There was an error saving comment %s', self.request.get('content'))

        logging.debug('Finish guestbook signing')
        self.redirect('/')


application = webapp.WSGIApplication([('/', MainPage),
                                      ('/sign', Guestbook)],
                                     debug=True)


def main():
    # Set the logging level in the main function
    # See the section on Requests and App Caching for information on how
    # App Engine reuses your request handlers when you specify a main function
    logging.getLogger().setLevel(logging.DEBUG)
    webapp.util.run_wsgi_app(application)

if __name__ == '__main__':
    main()

In the above example the logging method debug() is called to give us information about what requests our app handles. The info() method is called to give us information about the users of our application, and the error() method is called to let us know where our application is experiencing errors.

Later, we can use this information to filter out only the actions we are interested in. This will help us troubleshoot our code, improve the user experience, or add additional features to our application.

Access Application Logs in the Administration Console

To see the logs generated by your application, sign in to your Administration Console at https://appengine.google.com/ . In the left-hand navigation column, click Logs. You should see a summary of recent log events. To see more details about an event, click the [+] button on that event.

You can choose to view only those logged events that are above a certain severity. Debug events are considered the lowest severity, Critical events are considered the highest. For instance, if you select a minimum severity of 'Warning', all warning, error, and critical log events will be displayed.

To troubleshoot an event that took place at some point in the past, you can also specify a date and time period to jump to in the logs. Expand Options, enter a date and time range, and click Search. The logging console updates the view with logs at or before that time period.

Your application can control how much logs data to keep. To configure this, choose Application Settings in the Administration Console's side navigation, then find the Logs Retention section of that page. Choose the number of days of logs that the application should keep.

There is also a control showing the maximum amount of storage space to use for logs. (If your application does not have billing enabled, this control is grayed out and you can't change it.) When estimating how much space you need for logs, you might expect to need 100 bytes per request plus the content of any application logs.


Authentication required

You need to be signed in with Google+ to do that.

Signing you in...

Google Developers needs your permission to do that.