Error monitoring in Google app engine

Checking the GAE app engine logs each day for error messages can be very boring: what if you could receive a mail each time a non-handled Exception occurs in your Google app engine Java application ?

Well, it’s very easy: first define an generic “error handler” servlet in web.xml:

<servlet>
  <servlet-name>ShowErrorServlet</servlet-name>
  <servlet-class>fr.kubaski.servlet.ShowError</servlet-class>
</servlet>

<servlet-mapping>
  <servlet-name>ShowErrorServlet</servlet-name>
  <url-pattern>/showerror</url-pattern>
</servlet-mapping>

<error-page>
  <exception-type>java.lang.Exception</exception-type>
  <location>/showerror</location>
</error-page>

And here is the code for the servlet:

public class ShowError extends HttpServlet {
  private final Log log = LogFactory.getLog(getClass());

  @Override
  protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    Throwable t = (Throwable) req.getAttribute("javax.servlet.error.exception");
    log.error("error", t);
    try {
      Message msg = newMessage("theadmin@gmail.com", t.getMessage(), getDisplayableThrowable(t));
      Transport.send(msg);
    } catch (Exception e) {
      // we just log the error: this is not a big deal if the mail was not sent
      log.error("error", t);
    }
    getServletContext().getRequestDispatcher("/WEB-INF/jsp/error.jsp").forward(req, resp);
  }

  public static String getDisplayableThrowable(Throwable t) {
    StringWriter sw = new StringWriter();
    PrintWriter pw = new PrintWriter(sw, true);
    t.printStackTrace(pw);
    String result = sw.toString();
    try {
      sw.close();
    } catch (IOException e) { /* nothing */}
    pw.close();
    return result;
  }

  private Message newMessage(String adminMail, String subject, String body) throws MessagingException {
    Properties props = new Properties();
    Session session = Session.getDefaultInstance(props, null);
    Message msg = new MimeMessage(session);
    // As stated in GAE documentation:
    // "For security purposes, the sender address of a message must be the email address of an administrator for the application"
    msg.setFrom(new InternetAddress(adminMail));
    msg.addRecipient(Message.RecipientType.TO, new InternetAddress(adminMail));
    msg.setSubject(subject);
    msg.setText(body);
    return msg;
  }
}

Note that the mail sending feature does NOT work in “development mode” and that you need to deploy your app in GAE for this to work.

Laurent KUBASKI

Advertisements

About lkubaski
www.kubaski.com

2 Responses to Error monitoring in Google app engine

  1. Pingback: Real-time error monitoring in Google app engine « The skying cube

  2. Hi Laurent! You might take a look at Coalmine (www.getcoalmine.com). We have a Google App Engine connector that does this as well, but de-duplicates exceptions, makes them searchable, allows you to share them with your team, etc. Free to sign up.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: