Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
ydshah2 committed May 11, 2020
1 parent b400366 commit d008eb9
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 28 deletions.
24 changes: 12 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,22 +210,22 @@ properties.parse(config.DEFAULT_CONF_FILE, { path: true }, (err, parsedConfig) =

ws.on('open', function open() {
// Informational output for tail written to stderr to not interfere with actual data
utils.error('tail started. hosts: ' + (options.hosts || 'all') +
utils.log('tail started. hosts: ' + (options.hosts || 'all') +
'. apps: ' + (options.apps || 'all') +
'. tags: ' + (options.tags || 'all') +
'. levels: ' + (options.levels || 'all') +
'. query: ' + (query || 'none'));
'. query: ' + (query || 'none'), 'error');
});

ws.on('reconnecting', function(num) {
utils.error('tail reconnect attmpt #' + num + '...');
utils.log('tail reconnect attmpt #' + num + '...', 'error');
});

ws.on('message', function(data) {
try {
data = JSON.parse(data);
} catch (err) {
return utils.error('Malformed line: ' + err);
return utils.log('Malformed line: ' + err, 'error');
}

const account = data.e;
Expand All @@ -243,14 +243,14 @@ properties.parse(config.DEFAULT_CONF_FILE, { path: true }, (err, parsedConfig) =
err = err.toString();
if (err.indexOf('401') > -1) {
// invalid token
utils.error('Access token invalid. If you created or changed your password recently, please \'logdna login\' or \'logdna ssologin\' again. Type \'logdna --help\' for more info.');
utils.log('Access token invalid. If you created or changed your password recently, please \'logdna login\' or \'logdna ssologin\' again. Type \'logdna --help\' for more info.', 'error');
return process.exit();
}
utils.error('Error: ' + err);
utils.log('Error: ' + err, 'error');
});

ws.on('close', function() {
utils.error('tail lost connection');
utils.log('tail lost connection', 'error');
});
});

Expand Down Expand Up @@ -352,7 +352,7 @@ properties.parse(config.DEFAULT_CONF_FILE, { path: true }, (err, parsedConfig) =
var t, t2, range;

utils.apiGet(modifiedconfig, 'v1/export', params, function(error, body) {
if (error) return utils.error(error);
if (error) return utils.log(error, 'error');
if (body && body.range && body.range.from && body.range.to) {
t = new Date(body.range.from);
t2 = new Date(body.range.to);
Expand All @@ -370,17 +370,17 @@ properties.parse(config.DEFAULT_CONF_FILE, { path: true }, (err, parsedConfig) =
}).filter(element => element);
}
// Informational output for search written to stderr to not interfere with actual data
utils.error('search finished: ' + (body ? body.length : 0) + ' line(s)' + (range || '') +
utils.log('search finished: ' + (body ? body.length : 0) + ' line(s)' + (range || '') +
'. hosts: ' + (options.hosts || 'all') +
'. apps: ' + (options.apps || 'all') +
'. levels: ' + (options.levels || 'all') +
'. tags: ' + (options.tags || 'all') +
'. query: ' + (query || 'none'));
'. query: ' + (query || 'none'), 'error');


const lines = [].concat(body);
if (!lines.length) {
return utils.error('Query returned no lines.');
return utils.log('Query returned no lines.', 'error');
}

let last_timestamp = new Date(lines[0]._ts);
Expand All @@ -397,7 +397,7 @@ properties.parse(config.DEFAULT_CONF_FILE, { path: true }, (err, parsedConfig) =

config.last_timestamp = last_timestamp.toJSON();
utils.saveConfig(config, function(error, success) {
if (error) return utils.error(error);
if (error) return utils.log(error, 'error');
});
});
});
Expand Down
19 changes: 3 additions & 16 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,10 @@ const saveConfig = function(config, callback) {
});
};

const error = function(msg, config) {
const log = function(msg, level, config) {
try {
console.error(msg || '');
} catch (e) {
if (config) {
saveConfig(config, (error, result) => {
if (error) console.error(error);
return process.exit(0);
});
}
}
}

const log = function(msg, config) {
try {
console.log(msg || '');
if (level === 'error') console.error(msg || '');
else console.log(msg || '');
} catch (e) {
if (config) {
saveConfig(config, (error, result) => {
Expand Down Expand Up @@ -221,7 +209,6 @@ const performUpgrade = function(config, force, callback) {
};

exports.saveConfig = saveConfig;
exports.error = error;
exports.log = log;
exports.apiGet = apiGet;
exports.apiPost = apiPost;
Expand Down

0 comments on commit d008eb9

Please sign in to comment.