Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Write informational output to stderr for tail and search #43

Merged
4 commits merged into from
May 12, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,22 +209,23 @@ properties.parse(config.DEFAULT_CONF_FILE, { path: true }, (err, parsedConfig) =
var ws = new WebSocket((config.LOGDNA_APISSL ? 'https://' : 'http://') + config.LOGDNA_TAILHOST + '/ws/tail?' + qs.stringify(params));

ws.on('open', function open() {
// Informational output for tail written to stderr to not interfere with actual data
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'), null, 'error');
});

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

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

const account = data.e;
Expand All @@ -242,15 +243,14 @@ properties.parse(config.DEFAULT_CONF_FILE, { path: true }, (err, parsedConfig) =
err = err.toString();
if (err.indexOf('401') > -1) {
// invalid token
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.');
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.', null, 'error');
return process.exit();
}

utils.log('Error: ' + err);
utils.log('Error: ' + err, null, 'error');
});

ws.on('close', function() {
utils.log('tail lost connection');
utils.log('tail lost connection', null, '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.log(error);
if (error) return utils.log(error, null, '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 @@ -369,18 +369,18 @@ 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.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'), null, 'error');


const lines = [].concat(body);
if (!lines.length) {
return utils.log('Query returned no lines.');
return utils.log('Query returned no lines.', null, '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.log(error);
if (error) return utils.log(error, null, 'error');
});
});
});
Expand Down
8 changes: 6 additions & 2 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,13 @@ const saveConfig = function(config, callback) {
});
};

const log = function(msg, config) {
const log = function(msg, config, level) {
try {
console.log(msg || '');
if (level === 'error') {
console.error(msg || '');
} else {
console.log(msg || '');
}
} catch (e) {
if (config) {
saveConfig(config, (error, result) => {
Expand Down