Using Unified Logging with the Google App Engine

This post was sent in from a Unified Logging user who is leveraging it in the Google App Engine using python.

Overview

This post will go over the basics of hooking up Unified Logging with the Google App Engine (GAE).

Create and Configure the Google App Engine (GAE) Queue

1)  Add queue.yaml to the project root

2)  Configure queue by adding the following into queue.yaml

queue:

- name: unifiedlogging

 

Add Message to the Queue

Added the following method to my base handler class

def logMessage(self, message, severity):

#if using the GAE development server don’t send messages to UL.  

#Comment this out if you are testing or want the messages to be delivered

if ‘SERVER_SOFTWARE’ in os.environ and os.environ[‘SERVER_SOFTWARE’].startswith(‘Dev’):

                                    return

taskqueue.add(queue_name=’unifiedlogging’, url=’/unifiedlogging/log/’, params={‘message': message, ‘severity': severity})

Above illustrates the adding of a message to the unified logging queue that was defined above.  By specifying the url (/unifiedlogging/log/) it maps to what handler to use (discussed in Process the Message).  Lastly, pass the message and severity as parameters.

Process the Message

1)  In the app.yaml file add the following under the handlers: section

- url: /unifiedlogging/

script: task.app

Note that permissions can be specified for the queue.  Since this is for logging and we want to make sure all messages get in we leave it open.

2)  Create file called tasks.py.  This is where the handler is created to handle the UL message.  Think of a handler as controller.  The main difference between and handler and a controller is a controller you can have as many action methods as you want.  With the GAE your handler can only support having a method for get, post, put and delete.  It really is a restful unit of work.  You end up having a lot more handlers though the nice thing is you group your code nicely and it is easy to follow.   That is basically what you did in step 1.

3)  Create the handler.

import hashlib 

import json 

import sys 

import os       

import webapp2

from webapp2_extras.routes import RedirectRoute

from jinja2 import Environment

from jinja2.loaders import FileSystemLoader

from google.appengine.api import mail

from google.appengine.api import urlfetch

from baseHandler import BaseHandler

 

class UL(BaseHandler):   

def post(self):

            severity = self.getRequestValue(“severity”)

            message = self.getRequestValue(“message”)

            fullMessage = severity + “:” + message

            raw_message = {“Content”:fullMessage}

            jsonMessage = json.dumps(raw_message)

            md5 = fullMessage + “|” + secretKey

            utfencoded = md5.encode(‘utf-8′)

            shaEncoded = hashlib.sha1(utfencoded).hexdigest()

 

            result = urlfetch.fetch(url=”https://data-us.unifiedlogging.com/ulmessages/“,

                                    payload=jsonMessage,

                                    method=urlfetch.POST,

                                    headers={‘Content-Type': ‘application/json’,’Authorization':accessKey,’MD5′:shaEncoded})

 

status_code = result.status_code

            if status_code != 201:

                                    raise Exception(“Error calling UL:” + str(status_code))

 

app = webapp2.WSGIApplication([

                                    RedirectRoute(‘/unifiedlogging/log/’, UL, name=’ul’, strict_slash=True),

                                    ],debug=True,config=config.webapp2_config)

 

In your code write the below code to add a message to the queue:

self.logMessage(‘message’,’severity’)

 

The logMessage method was defined in Add Message to the Queue.

Note that the message can be formatted before it is put in the queue with additional information.

Comments are closed.