Skip to content

Commit

Permalink
Merge pull request #50 from nicoreed/master
Browse files Browse the repository at this point in the history
The logs command
  • Loading branch information
Nico Reed committed Jul 1, 2011
2 parents c2c98a2 + 9a68049 commit 011ff22
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 24 deletions.
33 changes: 29 additions & 4 deletions lib/jitsu/api/logs.js
Expand Up @@ -22,14 +22,39 @@ var Logs = exports.Logs = function (options) {
// Inherit from Client base object
util.inherits(Logs, jitsu.api.Client);

Logs.prototype.byApp = function (appId, callback) {
this.request('POST', ['logs', this.options.get('username') , appId], {}, callback, function (res, result) {
//
// ### function byApp (appId, amount, callback)
// #### @appId {string} the name of the application to retrieve
// #### @amount {number} the number of lines to retrieve
// #### @callback {function} Continuation to pass control to when complete.
// It retrieves the specified amount of logs for the application
//
Logs.prototype.byApp = function (appId, amount, callback) {
var options = {
from: 'NOW-3YEARS',
until: 'NOW',
rows: amount
};

this.request('POST', ['logs', this.options.get('username') , appId], options, callback, function (res, result) {
callback(null, result);
});
};

Logs.prototype.byUser = function (callback) {
this.request('POST', ['logs', this.options.get('username')], {}, callback, function (res, result) {
//
// ### function byUser (amount, callback)
// #### @amount {number} the number of lines to retrieve
// #### @callback {function} Continuation to pass control to when complete.
// It retrieves the specified amount of logs for all the applications for the user
//
Logs.prototype.byUser = function (amount, callback) {
var options = {
from: 'NOW-3YEARS',
until: 'NOW',
rows: amount
};

this.request('POST', ['logs', this.options.get('username')], options, callback, function (res, result) {
callback(null, result);
});
};
3 changes: 2 additions & 1 deletion lib/jitsu/commands/help.js
Expand Up @@ -50,6 +50,7 @@ help.usage = [
' jitsu apps',
' jitsu snapshots',
' jitsu users',
' jitsu logs',
' jitsu databases',
' jitsu conf',
' jitsu logout',
Expand Down Expand Up @@ -104,7 +105,7 @@ help.show = function (name, action) {
// Setup exports for all relevant resources:
// `apps`, `snapshots`, `config`.
//
['apps', 'databases', 'snapshots', 'users', 'config', 'list', 'deploy', 'create'].forEach(function (resource) {
['apps', 'databases', 'logs', 'snapshots', 'users', 'config', 'list', 'deploy', 'create'].forEach(function (resource) {
help[resource] = function (action, callback) {
if (!callback) {
callback = action;
Expand Down
87 changes: 68 additions & 19 deletions lib/jitsu/commands/logs.js
Expand Up @@ -4,75 +4,124 @@
* (C) 2010, Nodejitsu Inc.
*
*/

var winston = require('winston'),
jitsu = require('jitsu');

var logs = exports;

logs.usage = [
'`jitsu logs *` commands allow you to work with the log api',
'The `jitsu logs` commands allow you to read the logs related to your app.',
'The default number of lines to show is 10.',
'',
'Example usages:',
'jitsu logs all',
'jitsu logs all <number of lines to show>',
'jitsu logs app <app name>',
'jitsu logs app <app name> <number of lines to show>'
];

var parse = function (string) {
var pairs = string.split(',');
var obj = {}
for (var i = 0; i < pairs.length; ++i) {
var pair_split = pairs[i].split('=');
obj[pair_split[0]] = pair_split[1];
//
// ### function all (callback)
// #### @callback {function} Continuation to pass control to when complete.
// Queries the log API and retrieves the logs for all of the user's apps
//
logs.all = function (amount, callback) {
if (callback === undefined) {
callback = amount;
amount = 10;
}
return obj;
}

logs.all = function (databaseType, databaseName, callback) {
jitsu.logs.byUser( function (err, results) {
jitsu.logs.byUser(amount, function (err, results) {
if (err) {
callback(err);
}
else {
var logs = false;
for (key in results) {
for (var i = 0; i < results[key].data.length; ++i) {
logs = true;
var data = parse(results[key].data[i].text);
winston.info(results[key].data[i].timestamp.blue +
winston[data.level](results[key].data[i].timestamp.blue +
' ' + data['app'].yellow +
' ' + data['message']);
}
}

if (!logs) {
winston.info('No logs from timespan');
}

callback();
}
});
};

logs.all.usage = [
'Print the logs from all applications',
'Print the logs from all applications. The default number of',
'lines to show is 10.',
'jits logs all <number of lines to show>',
'',
'Example usage:',
'jitsu logs all',
'jitsu logs all 5'
]

logs.app = function (databaseName, callback) {
jitsu.logs.byApp(databaseName, function (err, results) {
//
// ### function app (appName, callback)
// #### @appName {string} the application to get the logs for
// #### @callback {function} Continuation to pass control to when complete.
// Queries the log API and retrieves the logs for the specified application
//
logs.app = function (appName, amount, callback) {
if (callback === undefined) {
callback = amount;
amount = 10;
}

jitsu.logs.byApp(appName, amount, function (err, results) {
if (err) {
callback(err);
}
else {
for (var i = 0; i < results.data.length; ++i) {
var data = parse(results.data[i].text);
winston.info(results.data[i].timestamp.blue +
winston[data.level](results.data[i].timestamp.blue +
' ' + data['app'].yellow +
' ' + data['message']);
}

if (results.data.length === 0) {
winston.info('No logs from timespan');
}

callback();
}
});
}

logs.app.usage = [
'Print the logs from specified application',
'Print the logs from specified application. The default number of',
'lines to show is 10.',
'jits logs app <app name> <number of lines to show>',
'',
'Example usage:',
'jitsu logs app <database name>'
'jitsu logs app test',
'jitsu logs app test 40'
]

//
// ### function parse (string)
// #### @string {string} string from logs
// It parses the output from the logs into an object
//
var parse = function (string) {
var pairs = string.split(',');
var obj = {};
for (var i = 0; i < pairs.length; ++i) {
var pair_split = pairs[i].split('=');
obj[pair_split[0]] = pair_split[1];
}
return obj;
}

0 comments on commit 011ff22

Please sign in to comment.