diff --git a/index.js b/index.js index d32b328..2cc4308 100644 --- a/index.js +++ b/index.js @@ -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; @@ -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'); }); }); @@ -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); @@ -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); @@ -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'); }); }); }); diff --git a/lib/utils.js b/lib/utils.js index 6196445..fc05056 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -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) => { @@ -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;