HTML5 Zone is brought to you in partnership with:

I am a software developer who specializes in web applications. My languages of choice are PHP and Javascript, but I dabble in a little bit of everything. I am the author of the Neo4jPHP binding for the Neo4j graph database. Every part of the software development process fascinates me. Josh is a DZone MVB and is not an employee of DZone and has posted 14 posts at DZone. You can read more from them at their website. View Full User Profile

Loggly from Javascript

04.16.2013
| 2774 views |
  • submit to reddit

For my most recent Dev Days project, I implemented centralized logging for our application, ServiceTrade. I don't want to worry about running our own indexing server, or storing the logs long term, so I investigated several SaaS logging solutions and eventually settled on Loggly. I was impressed with the ease of setting up our account, defining our logging inputs and even integrating with our Puppet configuration management infrastructure. For long term storage, they push raw log files to an S3 bucket of your choosing. Their customer support seemed very eager to help with the one issue I had. All-in-all, I've been pleased with the product.

One thing about Loggly that could use a little work is saved searches. First off, when Loggly gives you a graph of events from a saved search (a very cool feature) the graph sometimes loses information when zooming in and clicking on a section to see specific logs. Visiting the page for a saved search on a specific set of inputs and clicking on the graph to pull up the log lines for that search with give log lines across all inputs, not just the ones the saved search is limited to.

Secondly, you are limited to only 5 saved searches at the moment. The saved search feature is in beta, so hopefully they will allow saving of more (ideally unlimited) searches in the future.

Until that happens, we are using their excellent API to pull down data and do our own visualizations of multiple saved searches. I'm using jQuery on a simple HTML page to query the API. There are a few caveats. The following information will hopefully prevent someone else from spending the half-hour I did trying to figure this out.

First of all, the API uses HTTP basic authentication. Jquery's get call does not handle HTTP authentication, so I had to use the more verbose ajax method.

Also, since the request is cross-domain, I had to use JSONP, which Loggly supports.

Finally, Loggly's API returns a Bad Request response if you send any parameters that it does not recognize. Unfortunately, unless you tell it otherwise, jQuery.ajax() will always send a timestamp query parameter to prevent response caching. In order to get everything to work, I had to tell the request to turn caching off and not send the parameter.

Here is what the final call looks like:

$.ajax("https://mydomain.loggly.com/api/facets/date/?q=my_search_criteria", {
  // Don't send the _=timestamp query parameter
  cache: true,
  // Send HTTP auth credentials
  username: api_readonly_username,
  password: api_readonly_password,
  // Use JSONP
  dataType: "jsonp"
}).done(function(data) {
  // handle data, probably a call to some graphing library
}).fail(function() {
  // alert failure
});
I may do a blog post soon on using Puppet to automate setting up and servers with the correct syslog configs and automatically registering them with the Loggly API.



Published at DZone with permission of Josh Adell, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)