Skip to content

Commit

Permalink
[refactor breaking] Mostly done with refactor for V2 of Loggly API. O…
Browse files Browse the repository at this point in the history
…nly search and more tests remain.
  • Loading branch information
indexzero committed Oct 3, 2013
1 parent f2b7ff3 commit e32afcb
Show file tree
Hide file tree
Showing 7 changed files with 190 additions and 181 deletions.
5 changes: 2 additions & 3 deletions lib/loggly.js
Expand Up @@ -11,10 +11,9 @@ var loggly = exports;
//
// Export node-loggly core client APIs
//
loggly.createClient = require('./loggly/core').createClient;
loggly.createClient = require('./loggly/client').createClient;
loggly.serialize = require('./loggly/common').serialize;
loggly.Loggly = require('./loggly/core').Loggly;
loggly.Config = require('./loggly/config').Config;
loggly.Loggly = require('./loggly/client').Loggly;

//
// Export Resources for node-loggly
Expand Down
171 changes: 171 additions & 0 deletions lib/loggly/client.js
@@ -0,0 +1,171 @@
/*
* client.js: Core client functions for accessing Loggly
*
* (C) 2010 Nodejitsu Inc.
* MIT LICENSE
*
*/

var events = require('events'),
util = require('util'),
qs = require('querystring'),
Search = require('./search').Search,
loggly = require('../loggly');

//
// function createClient (options)
// Creates a new instance of a Loggly client.
//
exports.createClient = function (options) {
return new Loggly(options);
};

//
// ### function Loggly (options)
// #### @options {Object} Options for this Loggly client
// #### @subdomain
// #### @token
// #### @json
// #### @auth
// #### @tags
// Constructor for the Loggly object
//
var Loggly = exports.Loggly = function (options) {
if (!options || !options.subdomain || !options.token) {
throw new Error('options.subdomain and options.token are required.');
}

events.EventEmitter.call(this);

this.subdomain = options.subdomain;
this.token = options.token;
this.json = options.json || null;
this.auth = options.auth || null;

//
// Set the tags on this instance.
//
this.tags(options.tags);

var url = options.url || 'https://logs-01.loggly.com',
api = options.api || 'apiv2';

this.urls = {
default: url,
log: [url, 'inputs', this.token].join('/'),
api: [this.subdomain, 'loggly', 'com'].join('.') + '/' + api
};
};

//
// Inherit from events.EventEmitter
//
util.inherits(Loggly, events.EventEmitter);

//
// ### function log (msg, tags, callback)
// #### @msg {string|Object} Data to log
// #### @tags {Array} **Optional** Tags to send with this msg
// #### @callback {function} Continuation to respond to when complete.
// Logs the message to the token associated with this instance. If
// the message is an Object we will attempt to serialize it. If any
// `tags` are supplied they will be passed via the `X-LOGGLY-TAG` header.
// - http://www.loggly.com/docs/api-sending-data/
//
Loggly.prototype.log = function (msg, tags, callback) {
if (!callback && typeof tags === 'function') {
callback = tags;
tags = null;
}

if (msg instanceof Object) {
msg = this.config.json ? JSON.stringify(msg) : common.serialize(msg);
}
else {
msg = this.config.json ? JSON.stringify({ message : msg }) : msg;
}

var logOptions = {
headers: { 'Content-Type': this.config.json ? 'application/json' : 'text/plain' },
uri: this.urls.log,
method: 'POST',
body: msg
};

//
// Optionally send `X-LOGGLY-TAG` defaulting to the tags
// set on this instance.
//
tags = tags || this.tags;
if (Array.isArray(tags)) {
logOptions.headers['x-loggly-tag'] = tags.join(',');
}

common.loggly(logOptions, callback, function (res, body) {
try {
var result = JSON.parse(body);
emitter.emit('log', result);
if (callback) {
callback(null, result);
}
}
catch (ex) {
if (callback) {
callback(new Error('Unspecified error from Loggly: ' + ex));
}
}
});

return this;
};

//
// ### function tag (tags)
// #### @tags {Array} Tags to use for `X-LOGGLY-TAG`
// Sets the tags on this instance
//
Loggly.prototype.tags = function (tags) {
//
// TODO: Filter against valid tag names
// http://www.loggly.com/docs/tags/
//
this.tags = tags;
};


//
// ### function customer (callback)
// ### @callback {function} Continuation to respond to.
// Retrieves the customer information from the Loggly API:
// - http://www.loggly.com/docs/api-account-info/
//
Loggly.prototype.customer = function (callback) {
common.loggly(this.logglyUrl('customer'), callback);
};

//
// function search (query, callback)
// Returns a new search object which can be chained
// with options or called directly if @callback is passed
// initially.
//
// Sample Usage:
//
// client.search('404')
// .meta({ ip: '127.0.0.1' })
// .context({ rows: 100 })
// .run(function () { /* ... */ });
//
Loggly.prototype.search = function (query, callback) {
return new Search(query, this, callback);
};

//
// function logglyUrl ([path, to, resource])
// Helper method that concats the string params into a url
// to request against a loggly serverUrl.
//
Loggly.prototype.logglyUrl = function (/* path, to, resource */) {
var args = Array.prototype.slice.call(arguments);
return [this.logglyUrl].concat(args).join('/');
};
8 changes: 5 additions & 3 deletions lib/loggly/common.js
Expand Up @@ -62,6 +62,7 @@ common.loggly = function () {
var args = Array.prototype.slice.call(arguments),
success = args.pop(),
callback = args.pop(),
responded,
requestBody,
headers,
method,
Expand Down Expand Up @@ -100,8 +101,9 @@ common.loggly = function () {
}

function onError (err) {
if (callback) {
callback(err);
if (!responded) {
responded = true;
if (callback) { callback(err) }
}
}

Expand Down Expand Up @@ -143,7 +145,7 @@ common.loggly = function () {
// #### @obj {Object|literal} Object to serialize
// #### @key {string} **Optional** Optional key represented by obj in a larger object
// Performs simple comma-separated, `key=value` serialization for Loggly when
// logging to non-JSON inputs.
// logging for non-JSON values.
//
common.serialize = function (obj, key) {
if (obj === null) {
Expand Down
54 changes: 0 additions & 54 deletions lib/loggly/config.js

This file was deleted.

100 changes: 0 additions & 100 deletions lib/loggly/core.js

This file was deleted.

4 changes: 1 addition & 3 deletions test/helpers.js
Expand Up @@ -21,9 +21,7 @@ helpers.validConfig = function (config) {
&& config.auth
&& config.auth.username !== 'test-username'
&& config.auth.password !== 'test-password'
&& config.inputs
&& config.inputs.test
&& config.inputs.test_json;
&& config.token;
};

helpers.loadConfig = function () {
Expand Down

0 comments on commit e32afcb

Please sign in to comment.