You can schedule a cron job which can do this for you
First create a entry in cron.yaml
- description: Houston we have a problem Finder
url: /errorfinder/
schedule: every 3 hours
timezone: Asia/Kolkata
Then create an entry in app.yaml
handlers:
- url: /errorfinder/
script: errorfinder.app
secure: always
Now keep errorfinder.py with the following content
import base64
import datetime
import logging
import time
import urllib
import webapp2
from google.appengine.api.logservice import logservice
from google.appengine.api import mail
class MainHandler(webapp2.RequestHandler):
def get(self):
# Set up end time for our query.
end_time = time.time()
start_time = end_time - 10800 # 3 hours before now . same as cronjob interval
html = ''
report_needed = False
# Iterate through all the RequestLog objects, displaying some fields and
# iterate through all AppLogs beloging to each RequestLog count times
for req_log in logservice.fetch(start_time=start_time, end_time=end_time, minimum_log_level=logservice.LOG_LEVEL_WARNING, include_app_logs=True):
report_needed = True
html = html + '<br /> REQUEST LOG <br />'
html = html + 'IP: %s <br /> Method: %s <br /> Resource: %s <br />' % (req_log.ip, req_log.method, req_log.resource)
html = html + 'Date: %s<br />' % datetime.datetime.fromtimestamp(req_log.end_time).strftime('%D %T UTC')
for app_log in req_log.app_logs:
html = html + '<br />  APP LOG<br />'
html = html + '  Date: %s<br />' % datetime.datetime.fromtimestamp(app_log.time).strftime('%D %T UTC')
html = html + '  Message: <b>%s</b><br />' % app_log.message
html = html + '<br /><br /><br /><br />'
if(report_needed):
mail.send_mail(sender="Beagle Bot <bot@urdomain.com>",
to='lazyadmins@urdomain.com',
subject='Houston we have a problem ..',
body=html,
html=html,
reply_to='support@urdomain.com')
self.response.out.write(html)
app = webapp2.WSGIApplication([('/errorfinder/', MainHandler)], debug=True)
You can make the error search close to real time by reducing the cron job interval