Permalink
Browse files

[refcactor] Update to `jitsu logs *` commands to better parse data re…

…turned from Loggly
  • Loading branch information...
1 parent cd75d7e commit c36023f6d3eed6cdadd447d0a27fa5dfc688b345 @indexzero indexzero committed Aug 13, 2011
Showing with 83 additions and 42 deletions.
  1. +82 −42 lib/jitsu/commands/logs.js
  2. +1 −0 lib/jitsu/config.js
View
@@ -27,33 +27,37 @@ logs.usage = [
// Queries the log API and retrieves the logs for all of the user's apps
//
logs.all = function (amount, callback) {
- if (callback === undefined) {
+ if (!callback) {
callback = amount;
amount = 10;
}
- jitsu.logs.byUser(amount, function (err, results) {
+ jitsu.logs.byUser(amount, function (err, apps) {
if (err) {
- callback(err);
+ return 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[data.level](results[key].data[i].timestamp.blue +
- ' ' + data['app'].yellow +
- ' ' + data['message']);
- }
- }
- if (!logs) {
- winston.info('No logs from timespan');
+ if (apps.length === 0) {
+ winston.warn('No logs for ' + jitsu.config.get('username').magenta + ' from timespan');
+ return callback();
+ }
+
+ function sortLength (lname, rname) {
+ var llength = apps[lname].data.length,
+ rlength = apps[rname].data.length;
+
+ if (llength === rlength) {
+ return 0;
}
- callback();
+ return llength > rlength ? 1 : -1;
}
+
+ Object.keys(apps).sort(sortLength).forEach(function (app) {
+ putLogs(apps[app], app, amount, true);
+ });
+
+ callback();
});
};
@@ -65,7 +69,7 @@ logs.all.usage = [
'Example usage:',
'jitsu logs all',
'jitsu logs all 5'
-]
+];
//
// ### function app (appName, callback)
@@ -81,22 +85,11 @@ logs.app = function (appName, amount, callback) {
jitsu.logs.byApp(appName, amount, function (err, results) {
if (err) {
- callback(err);
+ return callback(err);
}
- else {
- for (var i = 0; i < results.data.length; ++i) {
- var data = parse(results.data[i].text);
- 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();
- }
+ putLogs(results, appName, amount);
+ callback();
});
}
@@ -108,20 +101,67 @@ logs.app.usage = [
'Example usage:',
'jitsu logs app test',
'jitsu logs app test 40'
-]
+];
+
+//
+// ### function putLogs (results, appName, amount, showApp)
+// #### @results {Object} Logs object to output.
+// #### @appName {string} App name associated with the log text.
+// #### @showApp {boolean} Value indicating if the app name should be output.
+// Parses, formats, and outputs the specified `results` to the user.
+//
+function putLogs (results, appName, amount, showApp) {
+ if (results.data.length === 0) {
+ return winston.warn('No logs for ' + appName.magenta + ' in specified timespan');
+ }
+
+ var logged = 0,
+ loglength = jitsu.config.get('loglength');
+
+ results.data.forEach(function (datum) {
+ var parsed = parse(datum.text);
+ if (parsed.text) {
+ parsed.text.split('\n').forEach(function (line) {
+ var prefix = showApp
+ ? datum.timestamp.grey + ' ' + parsed.app.magenta + ': '
+ : datum.timestamp.grey + ': '
+
+ if (line.length > 0 && logged < amount) {
+ if (line.length > loglength) {
+ line = line.substr(0, loglength - 3) + '...';
+ }
+
+ winston.data(prefix + line);
+ logged += 1;
+ }
+ });
+ }
+ });
+}
//
// ### 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];
+function parse (string) {
+ var parts = string.split(','),
+ parsed = {},
+ complex = false,
+ groups,
+ length;
+
+ for (var i = 0; i < parts.length; i++) {
+ groups = parts[i].split('=');
+ length = groups.length;
+
+ if (length > 2 || length === 0 || groups[0] === 'desc') {
+ parsed.text = groups.slice(1).join('');
+ }
+ else {
+ parsed[groups[0]] = groups[1];
+ }
}
- return obj;
-}
-
+
+ return parsed;
+}
View
@@ -27,6 +27,7 @@ var defaults = {
'root': process.env.HOME,
'protocol': 'http',
'remoteHost': 'api.nodejitsu.com',
+ 'loglength': 110,
'userconfig': '.jitsuconf',
'loglevel': 'info',
'tmproot': '/tmp',

0 comments on commit c36023f

Please sign in to comment.