Blog Node.js

Using the Loggly Node.js Library

By Hoover J. Beaver 20 Mar 2011

Loggly uses a fair amount of Node.js and it’s currently my favorite backend framework in which to develop cool mashups with Loggly. Last month Charlie Robbins of Nodejitsu finished up a fantastic library for doing Loggly searches, facet calls, and even sending in events to Loggly with Node.js. Charlie also posted about node-loggly’s release on Nodejitsu’s blog.

I’ve been talking about getting an analytics app built on top of Loggly for a while now, and figured this would be a good opportunity to try out node-loggly for querying Loggly facet info to drive graphs and charts. I’m already using it extensively for doing logging into Loggly from all my Node.js apps.

The entire example is hosted on Github and include instructions for getting it setup and going. Notable dependencies include Richard Henry’s awesome Paperboy static file module and Charlie’s library for Loggly.

The Code

The code below pulls a single Loggly facet call for the last week, compacts it into a single bucketed result, and then returns a simple JSON string to the client for handling. The last part logs use of the app itself to Loggly.

var apiHandler = function (req, resp) {
// parse our parameters
var url_parts = url.parse(req.url, true);
var query = url_parts.query['query'];

// run a facet call to loggly for the search term
geekceo.facet('date', query)
.context({ buckets: 1, from: "NOW-7DAYS", until: "NOW" })
.run(function (err, results) {
for (key in results.data) {}
var count = results.data[key];
var stuff = {'query': query, 'count': count};
resp.writeHead(200, { 'content-type': 'application/json', });
resp.write(JSON.stringify(stuff));
resp.end();
geekceo.log(lkey, 'loggly-node-chart: api request term='+query, lhandler);
});
}

Multiple facet searches are made for the terms defined in the index.html file.

var terms = ['safari', 'firefox', 'chrome', 'msie NOT 6.0', 'opera'];

Note that I’m not extracting the user-agent fields with these searches! I’m just doing a count of events with the term Safari, Chrome, etc. in them and then graphing the results. A better approach would be to extract a sampling of user-agents over a short time range, then search specifically for those strings.

Lastly, here are the views for the app itself during this post’s first hour of life. This was generated using a ‘graph loggly-node-chart’ in the Loggly shell:

I’ll continue to use Winston for all sorts of stuff moving forward. Please check out our Node.js Logging. and Node.js Error Handling articles.

Hoover J. Beaver

Hoover J. Beaver

Share Your Thoughts

Shares